@
zsj1029 @
koloonps 实现方式是用的缓冲流写入的,实际运行过程中,内存使用一直保持在 500MB 上下,代码如下
private static boolean writeFileToLocal(String toLocalFilePath, MultipartFile file) throws Exception {
boolean flag = false;
BufferedOutputStream bufferedOutputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(file.getInputStream());
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(toLocalFilePath));
int index;
byte[] bytes = new byte[4096];
while ((index = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, index);
bufferedOutputStream.flush();
}
flag = true;
} catch (IOException e) {
log.error("文件写入失败," + e.getMessage());
if (new File(toLocalFilePath).exists()) {
new File(toLocalFilePath).delete();
}
throw new Exception(e.getMessage());
} finally {
if (bufferedOutputStream != null) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.gc();
}