MultipartFile转file Java
将前端form-date提交的multipartFile文件类型转换为file类型以供操作
private static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
toFile = inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
private static File inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}