Hello大家好,本章我們新增多檔案上傳功能 。有問題可以聯絡我mr_beany@163.com。另求各路大神指點,感謝
在一個系統中,檔案上傳模組肯定是少不了的,例如頭像上傳,展示輪播圖等等,所以本章我們來新增上傳檔案功能
一:新增commons-fileupload依賴
開啟pom檔案新增
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>複製程式碼
二:新增系統變數
開啟core→constant檔案,新增檔案儲存路徑
//檔案上傳儲存的地址
public static final String SAVEFILEPATH = "F://img";複製程式碼
三:新增檔案上傳限制
建立core→configurer→MultipartConfigurer.java
package com.example.demo.core.configurer;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
@Configuration
public class MultipartConfigurer {
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory=new MultipartConfigFactory();
factory.setMaxFileSize("10MB");
factory.setMaxRequestSize("10MB");
return factory.createMultipartConfig();
}
}複製程式碼
四:建立檔案上傳工具類
建立core→utils→UploadActionUtil.java
package com.example.demo.core.utils;
import com.example.demo.core.constant.ProjectConstant;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author 張瑤
* 檔案上傳控制器
*/
public class UploadActionUtil {
public static List<String> uploadFile(HttpServletRequest request) throws Exception {
List<String> list = new ArrayList<>();
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
// 取得上傳檔案
MultipartFile file = multiRequest.getFile(iterator.next());
if (file != null) {
// 取得當前上傳檔案的檔名稱
String myFileName = file.getOriginalFilename();
// 如果名稱不為“”,說明該檔案存在,否則說明該檔案不存在
if (myFileName.trim() != "") {
String fileTyps = myFileName.substring(myFileName.lastIndexOf("."));
// String tempName="demo"+fileTyps;
String tempName = UUID.randomUUID().toString() + fileTyps;
// 建立資料夾
String folderPath = ProjectConstant.SAVEFILEPATH + File.separator + folderName();
File fileFolder = new File(folderPath);
if (!fileFolder.exists() && !fileFolder.isDirectory()) {
fileFolder.mkdir();
}
File uploadFile = new File(folderPath + File.separator + tempName);
file.transferTo(uploadFile);
myFileName = folderName() + File.separator + tempName;
list.add(ProjectConstant.SAVEFILEPATH + "//" + myFileName);
}
}
}
}
return list;
}
/**
* 得年月日的資料夾名稱
*
* @return
*/
public static String getCurrentFilderName() throws Exception{
Calendar now = Calendar.getInstance();
return now.get(Calendar.YEAR) + "" + (now.get(Calendar.MONTH) + 1) + "" + now.get(Calendar.DAY_OF_MONTH);
}
/**
* 建立資料夾
*
* @param filderName
*/
public static void createFilder(String filderName) throws Exception {
File file = new File(filderName);
// 如果資料夾不存在則建立
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
/**
* 副檔名
*
* @param fileName
* @return
*/
public static String extFile(String fileName) throws Exception{
return fileName.substring(fileName.lastIndexOf("."));
}
/**
* 當前日期當資料夾名
*
* @return
*/
public static String folderName() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String str = sdf.format(new Date());
return str;
}
}複製程式碼
五:建立UploadFileController
package com.example.demo.controller;
import com.example.demo.core.ret.RetResponse;
import com.example.demo.core.ret.RetResult;
import com.example.demo.core.utils.UploadActionUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
@RequestMapping("/uploadFile")
public class UploadFileController {
@PostMapping("/upload")
public RetResult<List<String>> upload(HttpServletRequest httpServletRequest) throws Exception {
List<String> list = UploadActionUtil.uploadFile(httpServletRequest);
return RetResponse.makeOKRsp(list);
}
}複製程式碼
六:測試
開啟postman
輸入localhost:8080/uploadFile/upload
注意:請求引數如下
返回值為檔案儲存的路徑
開啟設定好的儲存路徑
ok,上傳成功
專案地址
碼雲地址: gitee.com/beany/mySpr…
GitHub地址: github.com/MyBeany/myS…
寫文章不易,如對您有幫助,請幫忙點下star
結尾
新增多檔案上傳功能已完成,後續功能接下來陸續更新,有問題可以聯絡我mr_beany@163.com。另求各路大神指點,感謝大家。