.doc 파일에서 단어 메타 데이터를 제거하려고합니다. 내 .docx
파일은 XWPFDocument
으로 정상적으로 작동하지만 메타 데이터를 제거하는 다음 코드는 큰 파일 (> 1MB)에 실패합니다. 예를 들어 이미지가있는 6MB .doc 파일을 사용하면 일부 이미지가 제거 된 4.5MB 파일이 출력됩니다.Apache POI가 큰 워드 문서 파일 (HWPFDocument.write)을 저장하지 못함
public static InputStream removeMetaData(InputStream inputStream) throws IOException {
POIFSFileSystem fss = new POIFSFileSystem(inputStream);
HWPFDocument doc = new HWPFDocument(fss);
// **it even fails on large files if you remove from here to 'until' below**
SummaryInformation si = doc.getSummaryInformation();
si.removeAuthor();
si.removeComments();
si.removeLastAuthor();
si.removeKeywords();
si.removeSubject();
si.removeTitle();
doc.getDocumentSummaryInformation().removeCategory();
doc.getDocumentSummaryInformation().removeCompany();
doc.getDocumentSummaryInformation().removeManager();
try {
doc.getDocumentSummaryInformation().removeCustomProperties();
} catch (Exception e) {
// can not remove above
}
// until
ByteArrayOutputStream os = new ByteArrayOutputStream();
doc.write(os);
os.flush();
os.close();
return new ByteArrayInputStream(os.toByteArray());
}
관련 게시물 :
- How to save the Word Document using POI API?
- https://stackoverflow.com/questions/9758955/saving-poi-document-correctly
솔직히 말해서 문제가되지 않습니다. 문제가 발생했거나 예외가 발생했거나 출력 파일이 너무 큽니다.> 제거 할 항목이 아직 남아 있습니까? –
메타 데이터가 올바르게 제거되었지만 테이블, 바닥 글, 헤더 및 이미지가 포함 된 6MB 파일이있는 경우 poi는 메타 데이터가 제거 된 상태에서 ~ 4.5MB 파일을 출력하지만 임의의 이미지도 제거됩니다. – Mohsen
임의의 이미지가 제거되면 (각 호출은 다른 것을 제거함) 스레딩 문제이거나 부적절하게 넘겨진 메모리 부족 오류 일 수 있습니다. 스레딩 문제는 단일 스레드에서 실행하고 제대로 작동하는지 검사 할 수 있습니다. 브레이크 포인트로 인해 메모리 부족 오류를 발견 할 수 있습니다. 디버그 모드로 코드를 실행하고 오류 및 RuntimeExceptions를 잡아 내기 위해 catchpoint와 uncatched 예외 브레이크 포인트를 설정하는 것이 좋습니다. 어쩌면 당신은 뭔가 흥미로운 것을 발견 할 것입니다. –