public static void BatchUploadFile(MultipartHttpServletRequest multipartHttpServletRequest,String filePath) throws Exception {
MultiValueMap<String, MultipartFile> multiFileMap = multipartHttpServletRequest.getMultiFileMap();
Iterator<String> fileNames = multipartHttpServletRequest.getFileNames();
while(fileNames.hasNext()){
List<MultipartFile> multipartFiles = multiFileMap.get(fileNames.next());
for (MultipartFile multipartFile : multipartFiles) {
uploadFile(multipartFile,filePath);
}
}
}
public static String uploadFile(MultipartFile multipartFile,String filePath) throws Exception {
//获取文件名称
String originalFilename = multipartFile.getOriginalFilename();
//判断文件夹是存在
File fileDir = new File(filePath);
if (!fileDir.exists()){
fileDir.mkdirs();
}
try {
multipartFile.transferTo(new File(filePath+File.separator+originalFilename));
}catch (Exception e){
throw new Exception("异常");
}
return filePath+File.separator+originalFilename;
}
|