spring boot實(shí)現(xiàn)文件上傳
本文實(shí)例為大家分享了spring boot實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
一、簡介
java 中文件上傳涉及CommonsMultipartResolver 和 StandardServletMultipartResolver,其中CommonsMultipartResolver需要 commons-fileupload jar 包。StandardServletMultipartResolver 基于Servlet3.0 將不再需要任何額外的jar 包,tomcat 7.0 開始支持Servlet3.0。spring boot 2.0.4 內(nèi)嵌的tomcat 為8.5.32 。自動(dòng)配置信息如下:
用戶可自定義配置相關(guān)屬性。
二、流程
1.FileController.java
package com.vincent; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.UUID; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController? @RequestMapping("/file") public class FileController { ?? ?@PostMapping("/upload") ?? ?public String upload(MultipartFile multipartFile) { ?? ??? ? ?? ??? ?String base = "C:\\Users\\Administrator\\Desktop\\file\\"; ?? ??? ?File file = new File(base); ?? ??? ?if(!file.exists()) { ?? ??? ??? ?file.mkdirs(); ?? ??? ?} ?? ??? ? ?? ??? ?//獲取文件類型名 ?? ??? ?String[] parts = multipartFile.getOriginalFilename().split("\\."); ?? ??? ?String fileName = UUID.randomUUID().toString(); ?? ??? ? ?? ??? ?if(parts!= null && parts.length >= 2) { ?? ??? ??? ?fileName += "." + parts[parts.length-1]; ?? ??? ?} ?? ??? ?try { ?? ??? ??? ?multipartFile.transferTo(new File(base + fileName)); ?? ??? ?} catch (IllegalStateException | IOException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return "文件上傳失敗"; ?? ??? ?} ?? ??? ? ?? ??? ?return fileName; ?? ?} ?? ? ?? ?@GetMapping("/path") ?? ?public void file(String fileName,HttpServletResponse response) throws IOException { ?? ??? ? ?? ??? ?String base = "C:\\Users\\Administrator\\Desktop\\file\\"; ?? ??? ? ?? ??? ?byte[] bytes = Files.readAllBytes(Paths.get(base, fileName)); ?? ??? ?response.getOutputStream().write(bytes); ?? ??? ? ?? ?} ?? ? }
2.resources/static/html/index.html
<html> ?? ?<head> ?? ??? ?<meta charset="utf-8" > ?? ?</head> ?? ?<body> ?? ??? ?<form action="/file/upload" method="post" enctype="multipart/form-data"> ?? ??? ??? ?<input type="file" name="multipartFile"><br/> ?? ??? ??? ?<input type="submit" value="提交" />? ?? ??? ?</form> ?? ?</body> </html>
三、測試
1.訪問 http://localhost:8080/html/index.html
2.選擇文件并提交
3.使用 FileController 中請求獲取文件信息
四、多文件上傳
1.html form 的input 支持multiple 屬性,加上該屬性將可以上傳多個(gè)文件
2.多文件上傳的請求方法的MultipartFile 將是一個(gè)數(shù)組,遍歷該數(shù)組保存相關(guān)文件信息即可
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決springboot MultipartFile文件上傳遇到的問題
- 詳解SpringBoot文件上傳下載和多文件上傳(圖文)
- SpringBoot+Vue.js實(shí)現(xiàn)前后端分離的文件上傳功能
- springboot實(shí)現(xiàn)文件上傳和下載功能
- Spring boot 實(shí)現(xiàn)單個(gè)或批量文件上傳功能
- 詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
- springboot 文件上傳大小配置的方法
- Spring Boot + thymeleaf 實(shí)現(xiàn)文件上傳下載功能
- SpringBoot文件上傳控制及Java 獲取和判斷文件頭信息
- SpringBoot+layui實(shí)現(xiàn)文件上傳功能
相關(guān)文章
Mybatis MapperScannerConfigurer自動(dòng)掃描Mapper接口生成代理注入到Spring的方法
這篇文章主要給大家介紹了關(guān)于Mybatis MapperScannerConfigurer自動(dòng)掃描將Mapper接口生成代理注入到Spring的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2019-03-03java、python、JavaScript以及jquery循環(huán)語句的區(qū)別
本篇文章主要介紹java、python、JavaScript以及jquery的循環(huán)語句的區(qū)別,這里整理了它們循環(huán)語句語法跟示例,以便大家閱讀,更好的區(qū)分它們的不同2016-07-07mybatis 查詢sql中in條件用法詳解(foreach)
這篇文章主要介紹了mybatis 查詢sql中in條件用法詳解(foreach),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02java實(shí)現(xiàn)24點(diǎn)紙牌游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)24點(diǎn)紙牌游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03SpringBoot + Mybatis-plus實(shí)戰(zhàn)之Mybatis-plus的一級緩存、二級緩存
這篇文章主要介紹了SpringBoot + Mybatis-plus實(shí)戰(zhàn)之Mybatis-plus的一級緩存、二級緩存,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12