OfficeConvertController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package com.ruoyi.project.officeConvert;
  2. import com.ruoyi.common.constant.Constants;
  3. import com.ruoyi.common.constant.HttpStatus;
  4. import com.ruoyi.common.utils.StringUtils;
  5. import com.ruoyi.framework.config.RuoYiConfig;
  6. import com.ruoyi.framework.web.controller.BaseController;
  7. import com.ruoyi.framework.web.domain.AjaxResult;
  8. import org.jodconverter.DocumentConverter;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.web.bind.annotation.*;
  14. import org.springframework.web.context.request.RequestContextHolder;
  15. import org.springframework.web.context.request.ServletRequestAttributes;
  16. import javax.annotation.Resource;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.*;
  20. import java.nio.charset.StandardCharsets;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import static com.ruoyi.project.officeConvert.PptPreview.pptToImage;
  26. import static com.ruoyi.project.officeConvert.PptPreview.pptxToImage;
  27. @Component
  28. @RestController
  29. @RequestMapping(value="/office",method = RequestMethod.POST)
  30. public class OfficeConvertController {
  31. // 第一步:转换器直接注入
  32. @Resource
  33. DocumentConverter documentConverter;
  34. protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
  35. private static String successMsg="请求成功!请预览文件!";
  36. private static String failMsg="暂不支持此格式类型文件预览,请下载后查看!";
  37. private static String codeMiss="若csv文件预览结果有乱码,请重新上传编码格式另存为ANSI的csv文件!";
  38. private static String fileMiss="您需要预览的文件不存在,可能已在本地删除,请重新上传即可!";
  39. /**
  40. office类型的文件
  41. 先远程服务端获取文件
  42. 然后转换
  43. 然后读写缓冲区返回前台
  44. */
  45. @PostMapping("/toPdfFile")
  46. public AjaxResult toPdfFile(HttpServletRequest request, HttpServletResponse response,String filepath) {
  47. // 获取HttpServletResponse
  48. response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
  49. String newFilePath=filepath.replace("/profile","");
  50. // 需要转换的文件路径
  51. File file = new File(RuoYiConfig.getProfile()+newFilePath);
  52. boolean flag= file.exists();
  53. if(flag==false){
  54. return new AjaxResult(HttpStatus.SUCCESS,fileMiss,Constants.RESOURCE_PREFIX+ "/" + newFilePath);
  55. }
  56. String converterPdf =""; //生成的文件名
  57. try {
  58. // 转换之后文件生成的本地地址
  59. File newFile = new File(RuoYiConfig.getProfile());
  60. if (!newFile.exists()) {
  61. newFile.mkdirs();
  62. }
  63. String name =file.getName();
  64. int t=name.lastIndexOf(".");
  65. String newName=name.substring(0,t);
  66. //根据文件类型判断转换方式
  67. if(newFilePath.endsWith(".docx")||newFilePath.endsWith(".doc")||newFilePath.endsWith(".xlsx")
  68. ||newFilePath.endsWith(".xls")||newFilePath.endsWith(".csv")||newFilePath.endsWith(".txt")){
  69. converterPdf = newName+".html";
  70. }else if(newFilePath.endsWith(".mp4")||newFilePath.endsWith("pdf")||newFilePath.endsWith(".gif")
  71. ||newFilePath.endsWith("jpg")||newFilePath.endsWith("png")
  72. ||newFilePath.endsWith(".jpeg")){
  73. //浏览器可以直接打开的直接返回图片和视频的文件路径
  74. return new AjaxResult(HttpStatus.SUCCESS,successMsg,Constants.RESOURCE_PREFIX+ "/" + newFilePath) ;
  75. }else {
  76. //其他类型都不支持就返回错误消息
  77. return new AjaxResult(HttpStatus.SUCCESS,failMsg,Constants.RESOURCE_PREFIX+ "/" + newFilePath);
  78. }
  79. // 文件转化
  80. documentConverter.convert(file).to(new File(RuoYiConfig.getProfile() + File.separator +"temp" + File.separator +converterPdf)).execute();
  81. //office文件需要给有表格的加一个边框 以免 排版出现问题
  82. if(newFilePath.endsWith(".xlsx")){
  83. this.doActionConvertedFile(RuoYiConfig.getProfile() + File.separator +"temp" + File.separator +converterPdf);
  84. }
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. //最后应该返回转换后的文件名称
  89. String pathFileName = Constants.RESOURCE_PREFIX+ File.separator +"temp" + File.separator + converterPdf;
  90. return newFilePath.endsWith(".csv")? new AjaxResult(HttpStatus.SUCCESS,codeMiss,pathFileName): new AjaxResult(HttpStatus.SUCCESS,successMsg,pathFileName) ;
  91. }
  92. @PostMapping("/toBookFile")
  93. public AjaxResult toBookFile(HttpServletRequest request, HttpServletResponse response,String filepath) {
  94. // 获取HttpServletResponse
  95. response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
  96. String newFilePath=filepath.replace("/profile","");
  97. // 需要转换的文件路径
  98. File file = new File(RuoYiConfig.getProfile()+newFilePath);
  99. boolean flag= file.exists();
  100. if(flag==false){
  101. return new AjaxResult(HttpStatus.SUCCESS,fileMiss,Constants.RESOURCE_PREFIX+ "/" + newFilePath);
  102. }
  103. String converterPdf =""; //生成的文件名
  104. try {
  105. // 转换之后文件生成的本地地址
  106. File newFile = new File(RuoYiConfig.getProfile());
  107. if (!newFile.exists()) {
  108. newFile.mkdirs();
  109. }
  110. String name =file.getName();
  111. int t=name.lastIndexOf(".");
  112. String newName=name.substring(0,t);
  113. //预约开票的word文档
  114. converterPdf = newName+".pdf";
  115. // 文件转化
  116. documentConverter.convert(file).to(new File(RuoYiConfig.getProfile() + File.separator +"temp" + File.separator +converterPdf)).execute();
  117. //office文件需要给有表格的加一个边框 以免 排版出现问题
  118. // this.doActionConvertedFile(RuoYiConfig.getProfile() + File.separator +"temp" + File.separator +converterPdf);
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. //最后应该返回转换后的文件名称
  123. String pathFileName = Constants.RESOURCE_PREFIX+ File.separator +"temp" + File.separator + converterPdf;
  124. return new AjaxResult(HttpStatus.SUCCESS,successMsg,pathFileName) ;
  125. }
  126. public String wordTransPdf(String filepath) {
  127. // 获取HttpServletResponse
  128. String newFilePath=filepath.replace("/profile","");
  129. // 需要转换的文件路径
  130. File file = new File(RuoYiConfig.getProfile()+newFilePath);
  131. boolean flag= file.exists();
  132. if(flag==false){
  133. logger.error("文件不存在");
  134. }
  135. String converterPdf =""; //生成的文件名
  136. try {
  137. // 转换之后文件生成的本地地址
  138. File newFile = new File(RuoYiConfig.getProfile());
  139. if (!newFile.exists()) {
  140. newFile.mkdirs();
  141. }
  142. String name =file.getName();
  143. int t=name.lastIndexOf(".");
  144. String newName=name.substring(0,t);
  145. //预约开票的word文档
  146. converterPdf = newName+".pdf";
  147. // 文件转化
  148. documentConverter.convert(file).to(new File(RuoYiConfig.getProfile() + File.separator +"temp" + File.separator +converterPdf)).execute();
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152. //最后应该返回转换后的文件名称
  153. String pathFileName = Constants.RESOURCE_PREFIX+ File.separator +"temp" + File.separator + converterPdf;
  154. return pathFileName;
  155. }
  156. /**
  157. PPT文件需要根据不同格式类型区分一下
  158. */
  159. @PostMapping("/PPTransJPEG")
  160. public AjaxResult PPTransJPEG(HttpServletRequest request, HttpServletResponse response,String filepath) {
  161. Map<String, Object> resultMap = new HashMap<>();
  162. String newFilePath=filepath.replace("/profile","");
  163. // 需要转换的PPT文件路径
  164. File file = new File(RuoYiConfig.getProfile()+newFilePath);
  165. //判断ppt,pptx文件是否存在本地
  166. boolean flag= file.exists();
  167. if(flag==false){
  168. return new AjaxResult(HttpStatus.SUCCESS,fileMiss,Constants.RESOURCE_PREFIX+ "/" + newFilePath);
  169. }
  170. File imageFile = new File(RuoYiConfig.getProfile() + File.separator + "temp");
  171. if (!imageFile.exists()){
  172. imageFile.mkdirs();
  173. } //判断生成文件路径是否已存在
  174. String name =file.getName();
  175. int t=name.lastIndexOf(".");
  176. String newName=name.substring(0,t); //将名字带到生成方法中以便于区分不同文件名的ppt
  177. List list =new ArrayList();
  178. if(newFilePath.endsWith("pptx")){
  179. list= pptToImage(file, imageFile,newName);
  180. }else {
  181. list= pptxToImage(file, imageFile,newName);
  182. }
  183. //将生成的图片传给前端
  184. resultMap.put("imagePathList", list);
  185. // resultMap.put("reviewUrlPrefix", Constants.RESOURCE_PREFIX+ "/" );
  186. return new AjaxResult(HttpStatus.SUCCESS,successMsg,resultMap);
  187. }
  188. private static final String getPathFileName(String uploadDir, String fileName) throws IOException
  189. {
  190. int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
  191. String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
  192. String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
  193. return pathFileName;
  194. }
  195. /**
  196. * 对转换后的文件进行操作(改变编码方式)
  197. *
  198. * @param outFilePath 文件绝对路径
  199. */
  200. public void doActionConvertedFile(String outFilePath) {
  201. StringBuilder sb = new StringBuilder();
  202. try (InputStream inputStream = new FileInputStream(outFilePath);
  203. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, System.getProperty("sun.jnu.encoding")))) {
  204. String line;
  205. while (null != (line = reader.readLine())) {
  206. if (line.contains("charset=gb2312")) {
  207. line = line.replace("charset=gb2312", "charset=utf-8");
  208. }
  209. sb.append(line);
  210. }
  211. // 添加sheet控制头
  212. sb.append("<script src=\"js/jquery-3.0.0.min.js\" type=\"text/javascript\"></script>");
  213. sb.append("<script src=\"js/excel.header.js\" type=\"text/javascript\"></script>");
  214. sb.append("<link rel=\"stylesheet\" href=\"bootstrap/css/bootstrap.min.css\">");
  215. } catch (IOException e) {
  216. e.printStackTrace();
  217. }
  218. // 重新写入文件
  219. try (FileOutputStream fos = new FileOutputStream(outFilePath);
  220. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
  221. writer.write(sb.toString());
  222. } catch (IOException e) {
  223. e.printStackTrace();
  224. }
  225. }
  226. }