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

相关文章:

  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
  • 2021-06-14
  • 2022-03-08
  • 2020-02-09
  • 2022-12-23
猜你喜欢
  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
相关资源
相似解决方案