몬godB에 저장된 파일 미리 보기 (excel,word,PDF,그림)
4246 단어 파일 미리 보기
@RequestMapping(value = "/preview")
public void preview(String attachmentId) {
// mongodb id
Attachment attachment = attachmentService.getAttachmentDetail(attachmentId);
GridFSDBFile gridFSDBFile = attachmentService.getGridFSDBFile(attachment.getDbStoreId());
try {
//1、EXCEL( HTML )
HSSFWorkbook workBook = new HSSFWorkbook(gridFSDBFile.getInputStream());
ExcelToHtmlConverter converter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
converter.setOutputColumnHeaders(false);//
converter.setOutputRowNumbers(false);//
converter.processWorkbook(workBook);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(outStream);
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(new DOMSource(converter.getDocument()),
streamResult);
response.getOutputStream().write(outStream.toByteArray());
//2、word( HTML )
HWPFDocument wordDocument = new HWPFDocument(gridFSDBFile.getInputStream());
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content, PictureType pictureType,
String suggestedName, float widthInches, float heightInches) {
return "E:/test/"+suggestedName;
}
});
List pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
try {
pic.writeImageContent(new FileOutputStream("E:/test/"+pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
outStream.close();
String content = new String(outStream.toByteArray());
response.getOutputStream().write(outStream.toByteArray());
//3、PDF+TXT JPG JPEG
BufferedReader bis = null;
BufferedInputStream br = new BufferedInputStream(gridFSDBFile.getInputStream());
response.setHeader("Content-Disposition",
"inline; filename=" + URLEncoder.encode(gridFSDBFile.getFilename(), "UTF-8"));
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = br.read(buf)) != -1)
out.write(buf, 0, len);
br.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
몬godB에 저장된 파일 미리 보기 (excel,word,PDF,그림)텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.