全面解析SpringBoot文件上傳功能
這些天忙著刷題,又怕遺忘了spring boot, 所以抽出一點時間折騰折騰,加深點印象。
spring boot 的文件上傳與 spring mvc 的文件上傳基本一致,只需注意一些配置即可。
環(huán)境要求: Spring Boot v1.5.1.RELEASE + jdk1.7 + myeclipse
1).引入thymeleaf,支持頁面跳轉(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)頁面。先在templates 中新建一個 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 中寫兩個方法,一個方法跳轉(zhuǎn)到上傳文件的頁面,一個方法處理上傳文件
//跳轉(zhuǎn)到上傳文件的頁面
@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).在上面中,我將文件上傳的實現(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 測試

上傳文件后:

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

6).如果上傳的文件大于 1M 時,上傳會報錯文件太大的錯誤,在 application.properties 中設(shè)置上傳文件的參數(shù)即可
spring.http.multipart.maxFileSize=100Mb spring.http.multipart.maxRequestSize=100Mb
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis中example.createCriteria()方法的具體使用
本文詳細介紹了MyBatis的Example工具的使用方法,包括鏈?zhǔn)秸{(diào)用指定字段、設(shè)置查詢條件、支持多種查詢方式等,還介紹了mapper的crud方法、and/or方法的使用,以及如何進行多條件和多重條件查詢,感興趣的可以了解一下2024-10-10
從字節(jié)碼角度解析synchronized和反射實現(xiàn)原理
這篇文章主要介紹了從字節(jié)碼角度解析synchronized和反射的實現(xiàn)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
教新手使用java如何對一個大的文本文件內(nèi)容進行去重
用HashSet對內(nèi)容去重這個過程jvm會內(nèi)存溢出,只能首先將這個大文件中的內(nèi)容讀取出來,對每行String的hashCode取模取正整數(shù),可用取模結(jié)果作為文件名,將相同模數(shù)的行寫入同一個文件,再單獨對每個小文件進行去重,最后再合并2021-06-06
mybatis實現(xiàn)對數(shù)據(jù)的增刪查改實例詳解
這篇文章主要介紹了mybatis實現(xiàn)對數(shù)據(jù)的增刪查改實例詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07

