從FileInputStream讀取檔案頁碼後,檔案流被改變
阿新 • • 發佈:2019-02-08
如下程式碼,fis在讀取檔案頁碼操作後實際已被改變,若仍然用此物件,獲取到的md5有誤,為空或一固定值d41d8cd98f00b204e9800998ecf8427e
處理方法多設定一個fis2,用於後續操作
FileInputStream fis = (FileInputStream) f.getInputStream();
FileInputStream fis2 = (FileInputStream) f.getInputStream();try {
try {
if ("pdf".equals(fileType)) {
pageCount = MergePDF
.getNumberOfPages(fis);
} else if("docx".equals(fileType)) {
XWPFDocument docx = new XWPFDocument(fis);
pageCount = docx.getProperties()
.getExtendedProperties().getUnderlyingProperties()
.getPages();
}else{
WordExtractor doc = new WordExtractor(fis);
pageCount = doc.getSummaryInformation().getPageCount();//總頁數
}
} catch (Exception e) {
resultMap.put("message", "filepassword");
ResponseUtil.writeStringResponse(response,
FastJSONHelper.serialize(resultMap));
return ;
//pageCount = 0;
}
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024];
int length1 = -1;
while ((length1 = fis2.read(buffer, 0, 1024)) != -1) {
md.update(buffer, 0, length1);
}
BigInteger bigInt = new BigInteger(1, md.digest());
md5 = bigInt.toString(16);
System.out.println(md5);
} catch (Exception e) {
// e.printStackTrace();
}