全面解析SpringBoot文件上傳功能
這些天忙著刷題,又怕遺忘了spring boot, 所以抽出一點(diǎn)時(shí)間折騰折騰,加深點(diǎn)印象。
spring boot 的文件上傳與 spring mvc 的文件上傳基本一致,只需注意一些配置即可。
環(huán)境要求: Spring Boot v1.5.1.RELEASE + jdk1.7 + myeclipse
1).引入thymeleaf,支持頁(yè)面跳轉(zhuǎn)
<!-- 添加thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2).在 src/main/resources 目錄下新建 static 目錄和 templates 目錄。 static存放靜態(tài)文件,比如 css、js、image… templates 存放靜態(tài)頁(yè)面。先在templates 中新建一個(gè) uploadimg.html
<!DOCTYPE html> <html> <head> <title>uploadimg.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"></meta> <meta name="description" content="this is my page"></meta> <meta name="content-type" content="text/html; charset=UTF-8"></meta> <!--<link rel="stylesheet" type="text/css" href="./styles.css" rel="external nofollow" >--> </head> <body> <form enctype="multipart/form-data" method="post" action="/testuploadimg"> 圖片<input type="file" name="file"/> <input type="submit" value="上傳"/> </form> </body> </html>
3).在 controller 中寫兩個(gè)方法,一個(gè)方法跳轉(zhuǎn)到上傳文件的頁(yè)面,一個(gè)方法處理上傳文件
//跳轉(zhuǎn)到上傳文件的頁(yè)面
@RequestMapping(value="/gouploadimg", method = RequestMethod.GET)
public String goUploadImg() {
//跳轉(zhuǎn)到 templates 目錄下的 uploadimg.html
return "uploadimg";
}
//處理文件上傳
@RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
public @ResponseBody String uploadImg(@RequestParam("file") MultipartFile file,
HttpServletRequest request) {
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
/*System.out.println("fileName-->" + fileName);
System.out.println("getContentType-->" + contentType);*/
String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
try {
FileUtil.uploadFile(file.getBytes(), filePath, fileName);
} catch (Exception e) {
// TODO: handle exception
}
//返回json
return "uploadimg success";
}
4).在上面中,我將文件上傳的實(shí)現(xiàn)寫在工具類 FileUtil 的 uploadFile 方法中
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}
5).在瀏覽器輸入 :http://localhost:8080/gouploadimg 測(cè)試

上傳文件后:

在應(yīng)用的 src/main/webapp/imgupload 目錄下

6).如果上傳的文件大于 1M 時(shí),上傳會(huì)報(bào)錯(cuò)文件太大的錯(cuò)誤,在 application.properties 中設(shè)置上傳文件的參數(shù)即可
spring.http.multipart.maxFileSize=100Mb spring.http.multipart.maxRequestSize=100Mb
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot如何使用thymeleaf模板訪問(wèn)html頁(yè)面
- IDEA+maven+SpringBoot+JPA+Thymeleaf實(shí)現(xiàn)Crud及分頁(yè)
- springboot+thymeleaf國(guó)際化之LocaleResolver接口的示例
- 解決springboot MultipartFile文件上傳遇到的問(wèn)題
- SpringBoot+Vue.js實(shí)現(xiàn)前后端分離的文件上傳功能
- SpringBoot文件上傳控制及Java 獲取和判斷文件頭信息
- 詳解SpringBoot文件上傳下載和多文件上傳(圖文)
- springboot+thymeleaf 文件上傳功能的實(shí)現(xiàn)代碼
相關(guān)文章
Java基于Swing和netty實(shí)現(xiàn)仿QQ界面聊天小項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Java如何利用Swing和netty實(shí)現(xiàn)仿QQ界面聊天小項(xiàng)目,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-09-09
Java CountDownLatch完成異步回調(diào)實(shí)例詳解
這篇文章主要介紹了Java CountDownLatch完成異步回調(diào)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
SpringBoot集成Quartz實(shí)現(xiàn)持久化定時(shí)接口調(diào)用任務(wù)
Quartz是功能強(qiáng)大的開源作業(yè)調(diào)度庫(kù),幾乎可以集成到任何?Java?應(yīng)用程序中,從最小的獨(dú)立應(yīng)用程序到最大的電子商務(wù)系統(tǒng),本文將通過(guò)代碼示例給大家介紹SpringBoot集成Quartz實(shí)現(xiàn)持久化定時(shí)接口調(diào)用任務(wù),需要的朋友可以參考下2023-07-07
Mybatis中一條SQL使用兩個(gè)foreach的問(wèn)題及解決
這篇文章主要介紹了Mybatis中一條SQL使用兩個(gè)foreach的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot使用Jackson配置全局時(shí)間日期格式
本文主要介紹了SpringBoot使用Jackson配置全局時(shí)間日期格式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
java課程設(shè)計(jì)做一個(gè)多人聊天室(socket+多線程)
這篇文章主要介紹了我的java課程設(shè)計(jì)一個(gè)多人聊天室(socket+多線程)本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
關(guān)于jvm的垃圾回收器以及觸發(fā)full gc的場(chǎng)景
這篇文章主要介紹了關(guān)于jvm的垃圾回收器以及觸發(fā)full gc的場(chǎng)景,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04

