SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
@PostMapping("uploads")
public void uploads(MultipartFile[] multipartFiles, HttpServletRequest request){
List list = new ArrayList();
if (multipartFiles.length>0){
for (int i = 0; i < multipartFiles.length; i++) {
MultipartFile multipartFile = multipartFiles[i];
String realPath = "D:\\springboot\\uploadFile";
String time = sdf.format(new Date());
File file = new File(realPath+time);
if (!file.isDirectory()){
file.mkdirs();
}
String oldName = multipartFile.getOriginalFilename();
System.out.println("oldName="+oldName);
String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
System.out.println("newName="+newName);
try{
multipartFile.transferTo(new File(file,newName));
String filePath = request.getScheme() + "://" + request.getServerName() + ":"+ request.getServerPort() + "/uploadFile" + time + newName;
list.add(filePath);
}catch (Exception e){
e.printStackTrace();
}
}
System.out.println(list.toString());
}
System.out.println("请选择文件!");
}
多文件上传
最新推荐文章于 2024-05-23 18:27:55 发布