byte[] fileBytes = new byte[0];
try {
fileBytes = gasReportViolations.getAlarmImage().getBytes();
File tempFile = File.createTempFile("temp", ".png");
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(fileBytes);
File files = tempFile;
}catch (IOException e) {
e.printStackTrace();
//处理文件保存异常
return AjaxResult.error(0,"Failed to upload file");
}
gasReportViolations.getAlarmImage() 是Multipartfile类 .getBytes()方法是把Multipartfile转 byte【】
我这里使用的是先创建byte【】
File.createTempFile("temp", ".png"); 创建一个临时文件 .png结尾的文件
把文件写入 byte数组
在把数组分装成File类 就ok了