话不多说直接上代码
1前台通过name是images的file框选择后进入controller
@RequestMapping(value = "/image/save-test",method = RequestMethod.POST)
public String saveImg(@RequestParam("images") MultipartFile file)throws Exception {
String pathname = "";
String returnPath = "";
String fileName = file.getOriginalFilename();
System.out.println(fileName);
File path = new File(ResourceUtils.getURL("classpath:").getPath());
String absolutePath = path.getAbsolutePath();
System.out.println("path.getAbsolutePath()++++"+path.getAbsolutePath());
File uploadFile = new File(path.getAbsolutePath(), "static/images/");
System.out.println(uploadFile.getPath());
if (!uploadFile.exists()){
uploadFile.mkdirs();
}
String end=fileName.substring(fileName.length()-3,fileName.length());
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String name = df.format(new Date());
Random r = new Random();
for(int i = 0 ;i < 3 ;i++){
name += r.nextInt(10);
}
returnPath = name + "." +end;
String url="D:\\APP\\Hbuilde存储地址\\human\\spz\\images";
pathname = url+ "\\" + returnPath;
System.out.println("555555555"+pathname);
file.transferTo(new File(pathname));
return null;
}