multipartfile转化为file_MultipartFile 转为File 工具类

  • Post author:
  • Post category:其他


public 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());

inputStreamToFile(ins, toFile);

ins.close();

}

return toFile;

}

//获取流文件

private static void 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();

} catch (Exception e) {

e.printStackTrace();

}

}



版权声明:本文为weixin_35639680原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。