private void cheackFileTail(File file) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file,"rw");
//读文件头,不是jpg格式的不处理
byte[] headBytes = new byte[3];
randomAccessFile.read(headBytes,0,headBytes.length);
String headHex = StringUtil.byteToHex(headBytes);
if (!"ffd8ff".equals(headHex)){
return;
}
//读文件尾
randomAccessFile.seek(randomAccessFile.length() - 2);
byte[] tailBytes = new byte[2];
randomAccessFile.read(tailBytes);
String tailHex = StringUtil.byteToHex(tailBytes);
if (!tailHex.equals("ffd9")){
randomAccessFile.write(StringUtil.hexStringToBytes("ffd9"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (randomAccessFile != null){
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
检查文件信息尾
最新推荐文章于 2024-11-01 14:48:20 发布