java从minio下载大文件到磁盘目标位置,未下载完成时后缀使用.tmp,下载完成后修改文件名后缀
public Boolean downloadFileToLocalCache(String protocol, File file, String pathWithoutExtension) {
try{
String tmpPath = pathWithoutExtension + TMP_EXT;
File tmpFile = new File(tmpPath);
if (!tmpFile.exists() && storeService.exists(protocol)) {
try (InputStream in = storeService.getInputStream(protocol);
FileOutputStream out = new FileOutputStream(tmpFile)) {
IOUtils.copy(in, out);
}
tmpFile.renameTo(file);
}
} catch (Exception e){
log.warn("将材料缓存到服务器本地异常", e);
}
return true;
}
目前只有流关闭时才会写入磁盘,或者先写入一个0kb文件,流关闭时才会改变文件大小
换过使用BufferedOutputStream等其他流输出,或者自己写循环,并且添加os.flush()均没有效果
期望文件可以实时写入磁盘,并且文件大小也实时变化
byte[] b = new byte[1024];
int len = -1;
while((len = in.read(b))!=-1){
out.write(b,0,len);
out.flush();
}
这样写不行?文件大小不是实时变化的吗?
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!