OnlinePreviewController.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.yudianbank.web.controller;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yudianbank.param.ReturnResponse;
  5. import com.yudianbank.utils.DownloadUtils;
  6. import com.yudianbank.utils.FileUtils;
  7. import com.yudianbank.utils.OfficeToPdf;
  8. import com.yudianbank.utils.ZipReader;
  9. import org.apache.commons.io.IOUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.util.StringUtils;
  15. import org.springframework.web.bind.annotation.PathVariable;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.UnsupportedEncodingException;
  25. import java.net.HttpURLConnection;
  26. import java.net.URL;
  27. import java.net.URLConnection;
  28. import java.net.URLDecoder;
  29. /**
  30. * @author yudian-it
  31. */
  32. @Controller
  33. public class OnlinePreviewController {
  34. @Autowired
  35. private OfficeToPdf officeToPdf;
  36. @Autowired
  37. FileUtils fileUtils;
  38. @Autowired
  39. DownloadUtils downloadUtils;
  40. @Autowired
  41. ZipReader zipReader;
  42. @Value("${file.dir}")
  43. String fileDir;
  44. /**
  45. * @param url
  46. * @param model
  47. * @return
  48. */
  49. @RequestMapping(value = "onlinePreview",method = RequestMethod.GET)
  50. public String onlinePreview(String url, String needEncode, Model model, HttpServletRequest req) throws UnsupportedEncodingException {
  51. // 路径转码
  52. url = URLDecoder.decode(url, "utf-8");
  53. String type = typeFromUrl(url);
  54. String suffix = suffixFromUrl(url);
  55. model.addAttribute("fileType", suffix);
  56. if (type.equalsIgnoreCase("picture")) {
  57. model.addAttribute("imgurl", url);
  58. return "picture";
  59. } else if (type.equalsIgnoreCase("txt")
  60. || type.equalsIgnoreCase("html")
  61. || type.equalsIgnoreCase("xml")
  62. || type.equalsIgnoreCase("java")
  63. || type.equalsIgnoreCase("properties")
  64. || type.equalsIgnoreCase("mp3")){
  65. model.addAttribute("ordinaryUrl",url);
  66. return "txt";
  67. } else if(type.equalsIgnoreCase("pdf")){
  68. model.addAttribute("pdfUrl",url);
  69. return "pdf";
  70. } else if(type.equalsIgnoreCase("compress")){
  71. // 抽取文件并返回文件列表
  72. String fileName = fileUtils.getFileNameFromURL(url);
  73. String fileTree = null;
  74. // 判断文件名是否存在(redis缓存读取)
  75. if (!StringUtils.hasText(fileUtils.getConvertedFile(fileName))) {
  76. ReturnResponse<String> response = downloadUtils.downLoad(url, suffix, fileName, needEncode);
  77. if (0 != response.getCode()) {
  78. model.addAttribute("msg", response.getMsg());
  79. return "fileNotSupported";
  80. }
  81. String filePath = response.getContent();
  82. if ("zip".equalsIgnoreCase(suffix)
  83. || "jar".equalsIgnoreCase(suffix)
  84. || "gzip".equalsIgnoreCase(suffix)) {
  85. fileTree = zipReader.readZipFile(filePath);
  86. } else if ("rar".equalsIgnoreCase(suffix)) {
  87. fileTree = zipReader.unRar(filePath);
  88. }
  89. fileUtils.addConvertedFile(fileName, fileTree);
  90. }else {
  91. fileTree = fileUtils.getConvertedFile(fileName);
  92. }
  93. System.out.println("返回文件tree》》》》》》》》》》》》》》》》》》》");
  94. if (null != fileTree) {
  95. model.addAttribute("fileTree",fileTree);
  96. return "compress";
  97. }else {
  98. model.addAttribute("msg", "压缩文件类型不受支持,尝试在压缩的时候选择RAR4格式");
  99. return "fileNotSupported";
  100. }
  101. } else if ("office".equalsIgnoreCase(type)) {
  102. String fileName = fileUtils.getFileNameFromURL(url);
  103. boolean isHtml = suffix.equalsIgnoreCase("xls")
  104. || suffix.equalsIgnoreCase("xlsx");
  105. String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
  106. // 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
  107. if (!fileUtils.listConvertedFiles().containsKey(pdfName)) {
  108. String filePath = fileDir + fileName;
  109. if (!new File(filePath).exists()) {
  110. ReturnResponse<String> response = downloadUtils.downLoad(url, suffix, null, needEncode);
  111. if (0 != response.getCode()) {
  112. model.addAttribute("msg", response.getMsg());
  113. return "fileNotSupported";
  114. }
  115. filePath = response.getContent();
  116. }
  117. String outFilePath = fileDir + pdfName;
  118. if (StringUtils.hasText(outFilePath)) {
  119. officeToPdf.openOfficeToPDF(filePath, outFilePath);
  120. File f = new File(filePath);
  121. if (f.exists()) {
  122. f.delete();
  123. }
  124. if (isHtml) {
  125. // 对转换后的文件进行操作(改变编码方式)
  126. fileUtils.doActionConvertedFile(outFilePath);
  127. }
  128. // 加入缓存
  129. fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
  130. }
  131. }
  132. model.addAttribute("pdfUrl", pdfName);
  133. return isHtml ? "html" : "pdf";
  134. }else {
  135. model.addAttribute("msg", "系统还不支持该格式文件的在线预览," +
  136. "如有需要请按下方显示的邮箱地址联系系统维护人员");
  137. return "fileNotSupported";
  138. }
  139. }
  140. private String suffixFromUrl(String url) {
  141. String nonPramStr = url.substring(0, url.indexOf("?") != -1 ? url.indexOf("?"): url.length());
  142. String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
  143. String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
  144. return fileType;
  145. }
  146. /**
  147. * 查看文件类型(防止参数中存在.点号或者其他特殊字符,所以先抽取文件名,然后再获取文件类型)
  148. * @param url
  149. * @return
  150. */
  151. private String typeFromUrl(String url) {
  152. String nonPramStr = url.substring(0, url.indexOf("?") != -1 ? url.indexOf("?"): url.length());
  153. String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
  154. String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
  155. if (fileUtils.listPictureTypes().contains(fileType.toLowerCase())) {
  156. fileType = "picture";
  157. }
  158. if (fileUtils.listArchiveTypes().contains(fileType.toLowerCase())) {
  159. fileType = "compress";
  160. }
  161. if (fileUtils.listOfficeTypes().contains(fileType.toLowerCase())) {
  162. fileType = "office";
  163. }
  164. return fileType;
  165. }
  166. /**
  167. * 根据url获取文件内容
  168. * 当pdfjs读取存在跨域问题的文件时将通过此接口读取
  169. * @param urlPath
  170. * @param resp
  171. */
  172. @RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
  173. public void getCorsFile(String urlPath, HttpServletResponse resp) {
  174. InputStream inputStream = null;
  175. try {
  176. String strUrl = urlPath.trim();
  177. URL url=new URL(strUrl);
  178. //打开请求连接
  179. URLConnection connection = url.openConnection();
  180. HttpURLConnection httpURLConnection=(HttpURLConnection) connection;
  181. httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  182. inputStream = httpURLConnection.getInputStream();
  183. byte[] bs = new byte[1024];
  184. int len;
  185. while(-1 != (len = inputStream.read(bs))) {
  186. resp.getOutputStream().write(bs, 0, len);
  187. }
  188. } catch (IOException e) {
  189. e.printStackTrace();
  190. } finally {
  191. if(inputStream != null) {
  192. IOUtils.closeQuietly(inputStream);
  193. }
  194. }
  195. }
  196. }