问题:MappedByteBuffer内存占用和文件关闭等不确定问题,被MappedByteBuffer打开的文件只有在垃圾收集时才会被关闭。
业务场景
定时任务:操作文件
(new CreateCPWordToFSExecutorTask(), 0, PERIOD_DAY, );//(按推迟时间间隔执行)
CreateCPWordToFSExecutorTask类中获取文件的
FileMD5 md5 = new FileMD5();
("MD5", md5.getMd5ByFile(file));
问题代码 注释段
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* @ClassName: FileMD5
* @Description: 文件的MD5
* @author: zcj
* @date: 2017年5月19日 下午2:59:21
*/
public class FileMD5 {
/**
*
* @Title: getMd5ByFile
* @Description: MappedByteBuffer可以提高效率,但也存在一些问题,主要就是内存占用和文件关闭等不确定问题,
* 被MappedByteBuffer打开的文件只有在垃圾收集时才会被关闭。。。
* @param file
* @return
* @return: String
*/
public String getMd5ByFile(File file) {
String value = null;
FileInputStream inputStream = null;
FileChannel inFileChannel = null;
try {
inputStream = new FileInputStream(file);
inFileChannel = ();
//MappedByteBuffer byteBuffer = (.READ_ONLY, 0, ());
MessageDigest md5 = ("MD5");
byte[] byteBuffer = new byte[1024];
int length = -1;
while ((length = (byteBuffer, 0, 1024)) != -1) {
(byteBuffer, 0, length);
}
//(byteBuffer);
BigInteger bi = new BigInteger(1, ());
value = (16);
} catch (Exception e) {
();
}finally {
if (null != inFileChannel) {
try {
();
} catch (IOException e) {
();
}
}
if (null != inputStream) {
try {
();
} catch (IOException e) {
();
}
}
}
try {
(100);
} catch (InterruptedException e) {
();
}
return ();
}
}
执行备注是代码异常:: D:\test\档案卡_20170619.doc (请求的操作无法在使用用户映射区域打开的文件上执行。)
参考:/blog/2107023