|
@@ -394,13 +394,29 @@ public class DocInfoController extends BaseController {
|
|
|
@Log(title = "PDF转WORD", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
|
|
|
@PostMapping("/pdf2word")
|
|
|
public void pdf2word(@ApiParam(value = "文件", required = true) @RequestPart(value = "file") MultipartFile file, HttpServletResponse response) {
|
|
|
- try (ServletOutputStream os = response.getOutputStream(); Document doc = new Document(file.getInputStream());) {
|
|
|
+ try (Document doc = new Document(file.getInputStream());) {
|
|
|
+ write(doc, file.getOriginalFilename(), response);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("Pdf 转 Word 失败{}", e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入文件流
|
|
|
+ * @param doc doc
|
|
|
+ * @param fileName 文件流
|
|
|
+ * @param response res
|
|
|
+ */
|
|
|
+ private void write(Document doc, String fileName, HttpServletResponse response) {
|
|
|
+ try (ServletOutputStream os = response.getOutputStream();) {
|
|
|
//doc是将要被转化的word文档
|
|
|
+ String disposition = "attachment; filename=\"" + UriEncoder.encode(fileName) + "\"";
|
|
|
+ response.addHeader(HttpHeaders.CONTENT_DISPOSITION, disposition);
|
|
|
+ response.addHeader(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
|
|
|
doc.save(os, SaveFormat.DocX);
|
|
|
- String disposition = "attachment; filename=\"" + UriEncoder.encode(file.getOriginalFilename()) + "\"";
|
|
|
- response.addHeader(HttpHeaders.CONTENT_DISPOSITION, disposition);
|
|
|
- response.addHeader(HttpHeaders.CONTENT_TYPE, ".docx");
|
|
|
os.flush();
|
|
|
} catch (Exception e) {
|
|
|
logger.error("Pdf 转 Word 失败{}", e.getMessage());
|
|
@@ -414,14 +430,8 @@ public class DocInfoController extends BaseController {
|
|
|
@GetMapping("/pdf2word/{fileId}")
|
|
|
public void pdf2word(@PathVariable(name = "fileId") String fileId, HttpServletResponse response) {
|
|
|
DocumentVO vo = mongoService.downloadFile(fileId);
|
|
|
- try (ServletOutputStream os = response.getOutputStream(); Document doc = new Document(vo.getData());) {
|
|
|
- //doc是将要被转化的word文档
|
|
|
- //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
|
|
|
- doc.save(os, SaveFormat.DocX);
|
|
|
- String disposition = "attachment; filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"";
|
|
|
- response.addHeader(HttpHeaders.CONTENT_DISPOSITION, disposition);
|
|
|
- response.addHeader(HttpHeaders.CONTENT_TYPE, ".docx");
|
|
|
- os.flush();
|
|
|
+ try (Document doc = new Document(vo.getData())) {
|
|
|
+ write(doc, vo.getFileName(), response);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("Pdf 转 Word 失败{}", e.getMessage());
|
|
|
e.printStackTrace();
|