@RequestMapping("/fileUpload")
public void fileUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
try {
byte[] fileByte = file.getBytes();
String oldFileName = file.getOriginalFilename();
String filePath = "D://upload//";
Date date = new Date();
String newFileName = String.valueOf(date.getTime());
Date time = Calendar.getInstance().getTime();
String suffixName = oldFileName.substring(oldFileName.lastIndexOf("."));
newFileName = newFileName + suffixName;
boolean flag = uploadFile(fileByte, filePath, newFileName);
} catch (IOException ioException) {
}
}
public static boolean uploadFile(byte[] file, String filePath, String fileName) {
boolean flag = true;
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(filePath + fileName);
fileOutputStream.write(file);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
System.out.println("文件不存在异常");
flag = false;
} catch (IOException ioException) {
System.out.println("javaIO流异常");
flag = false;
}
return flag;
}