PdfController.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package com.uas.report.controller;
  2. import com.uas.report.model.ExportType;
  3. import com.uas.report.model.Platform;
  4. import com.uas.report.service.FileService;
  5. import com.uas.report.service.PrintService;
  6. import com.uas.report.util.*;
  7. import net.sf.jasperreports.engine.JRException;
  8. import org.dom4j.DocumentException;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.sql.SQLException;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. /**
  22. * 获取pdf
  23. *
  24. * @author sunyj
  25. * @since 2017年8月10日 下午7:17:38
  26. */
  27. @Controller
  28. @RequestMapping("/pdf")
  29. public class PdfController {
  30. @Autowired
  31. private PrintService printService;
  32. @Autowired
  33. private FileService fileService;
  34. /**
  35. * @param u
  36. * 当前账套名称,不可为空
  37. * @param pr
  38. * 用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev,可选(UAS等系统不必传递该参数)
  39. * @param r
  40. * 需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase"),不可为空
  41. * @param w
  42. * where之后的条件(包括where),可为空
  43. * @param o
  44. * 其他参数,区别于w,报表某些字段的值取决于这些参数, JSON格式,数据为键值对,若模板已指定需要的参数,则不可为空
  45. * @param pf
  46. * 请求来自的平台,不可为空
  47. * @param request
  48. * @param response
  49. * @return JSON格式
  50. *
  51. * <table border=1 cellpadding=5 cellspacing=0 summary= "result">
  52. * <tr>
  53. * <th>key</th>
  54. * <th>description</th>
  55. * </tr>
  56. * <tr>
  57. * <td>pdf</td>
  58. * <td>pdf相对路径</td>
  59. * </tr>
  60. * <tr>
  61. * <td>pageSize</td>
  62. * <td>pdf页数</td>
  63. * </tr>
  64. * <tr>
  65. * <td>overload</td>
  66. * <td>数据量是否过大</td>
  67. * </tr>
  68. * </table>
  69. * @throws JRException
  70. * @throws IOException
  71. * @throws DocumentException
  72. * @throws SQLException
  73. */
  74. @RequestMapping(value = "/path")
  75. @ResponseBody
  76. public Map<String, Object> getPath(@RequestParam(required = true) String u, String pr,
  77. @RequestParam(required = true) String r, String w, String o, @RequestParam(required = true) String pf,
  78. HttpServletRequest request, HttpServletResponse response)
  79. throws JRException, IOException, DocumentException, SQLException {
  80. u = u == null ? null : u.toUpperCase();
  81. ReportUtils.checkParameters(u, r);
  82. Map<String, Object> result = new HashMap<>();
  83. // 判断是否过载
  84. if (printService.overload(u, pr, r, w, o, Platform.checkPlatform(pf))) {
  85. result.put("path", "");
  86. result.put("pageSize", 0);
  87. result.put("overload", true);
  88. } else {
  89. result = printService.preview(u, pr, r, w, o, null);
  90. if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
  91. throw new IllegalStateException("pdf生成失败:" + u + "/" + r);
  92. }
  93. byte[] data = (byte[]) result.remove("data");
  94. // 相对路径
  95. String pdfPath = r + "/"
  96. + fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier())
  97. + "." + ExportType.PDF.getQualifier();
  98. File file = new File(ReportUtils.getDocumentDir(), pdfPath);
  99. FileUtils.write(file.getPath(), data);
  100. result.put("path", "pdf/preview?p=" + pdfPath);
  101. result.put("overload", false);
  102. }
  103. return result;
  104. }
  105. /**
  106. * @param u
  107. * 当前账套名称,不可为空
  108. * @param pr
  109. * 用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev,可选(UAS等系统不必传递该参数)
  110. * @param r
  111. * 需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase"),不可为空
  112. * @param w
  113. * where之后的条件(包括where),可为空
  114. * @param o
  115. * 其他参数,区别于w,报表某些字段的值取决于这些参数, JSON格式,数据为键值对,若模板已指定需要的参数,则不可为空
  116. * @param pf
  117. * 请求来自的平台,不可为空
  118. * @param request
  119. * @param response
  120. * @return JSON格式
  121. *
  122. * <table border=1 cellpadding=5 cellspacing=0 summary= "result">
  123. * <tr>
  124. * <th>key</th>
  125. * <th>description</th>
  126. * </tr>
  127. * <tr>
  128. * <td>data</td>
  129. * <td>pdf的字节数据</td>
  130. * </tr>
  131. * <tr>
  132. * <td>pageSize</td>
  133. * <td>pdf页数</td>
  134. * </tr>
  135. * <tr>
  136. * <td>overload</td>
  137. * <td>数据量是否过大</td>
  138. * </tr>
  139. * </table>
  140. * @throws JRException
  141. * @throws IOException
  142. * @throws DocumentException
  143. * @throws SQLException
  144. */
  145. @RequestMapping(value = "/data")
  146. @ResponseBody
  147. public Map<String, Object> getData(@RequestParam(required = true) String u, String pr,
  148. @RequestParam(required = true) String r, String w, String o, @RequestParam(required = true) String pf,
  149. HttpServletRequest request, HttpServletResponse response)
  150. throws JRException, IOException, DocumentException, SQLException {
  151. u = u == null ? null : u.toUpperCase();
  152. ReportUtils.checkParameters(u, r);
  153. Map<String, Object> result = new HashMap<>();
  154. // 判断是否过载
  155. if (printService.overload(u, pr, r, w, o, Platform.checkPlatform(pf))) {
  156. result.put("data", "");
  157. result.put("pageSize", 0);
  158. result.put("overload", true);
  159. } else {
  160. result = printService.preview(u, pr, r, w, o, null);
  161. if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
  162. throw new IllegalStateException("pdf生成失败:" + u + "/" + r);
  163. }
  164. result.put("overload", false);
  165. }
  166. return result;
  167. }
  168. /**
  169. * 下载 pdf (断点续传)
  170. *
  171. * @param u
  172. * 当前账套名称,不可为空
  173. * @param pr
  174. * 用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev,可选(UAS等系统不必传递该参数)
  175. * @param r
  176. * 需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase"),不可为空
  177. * @param w
  178. * where之后的条件(包括where),可为空
  179. * @param o
  180. * 其他参数,区别于w,报表某些字段的值取决于这些参数, JSON格式,数据为键值对,若模板已指定需要的参数,则不可为空
  181. * @param pf
  182. * 请求来自的平台,不可为空
  183. * @param n
  184. * 下载后的文件名称,可为空
  185. * @param request
  186. * @param response
  187. * @throws JRException
  188. * @throws IOException
  189. * @throws DocumentException
  190. * @throws SQLException
  191. * @throws IllegalStateException
  192. */
  193. @RequestMapping(value = "/download")
  194. @ResponseBody
  195. public void download(@RequestParam(required = true) String u, String pr, @RequestParam(required = true) String r,
  196. String w, String o, @RequestParam(required = true) String pf, String n, HttpServletRequest request,
  197. HttpServletResponse response)
  198. throws JRException, IOException, DocumentException, SQLException, IllegalStateException {
  199. u = u == null ? null : u.toUpperCase();
  200. ReportUtils.checkParameters(u, r);
  201. // 相对路径
  202. String pdfPath = r + "/"
  203. + fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier())
  204. + "." + ExportType.PDF.getQualifier();
  205. File file = new File(ReportUtils.getDocumentDir(), pdfPath);
  206. String masterOfJrxml = printService.getMasterOfJrxml(u, r);
  207. String jrxmlFilePath = fileService.getJrxmlFilePath(masterOfJrxml, r);
  208. if (!fileService.isFileValid(file.getPath(), jrxmlFilePath)) {
  209. Map<String, Object> map = getPath(u, pr, r, w, o, pf, request, response);
  210. Boolean overload = (Boolean) map.get("overload");
  211. if (overload) {
  212. throw new IllegalStateException("数据量过大,无法提供服务");
  213. }
  214. }
  215. fileService.rangeDownload(file, n == null ? null : n + "."+ExportType.PDF.getQualifier(), request, response);
  216. }
  217. /**
  218. * 预览 pdf (Content Type 为 application/pdf)
  219. *
  220. * @param p
  221. * pdf 相对路径
  222. * @param request
  223. * @param response
  224. */
  225. @RequestMapping(value = "/preview")
  226. @ResponseBody
  227. public void preview(@RequestParam String p, HttpServletRequest request, HttpServletResponse response)
  228. throws JRException, IOException, DocumentException, SQLException, IllegalStateException {
  229. File file = new File(ReportUtils.getDocumentDir(), p);
  230. if(!file.getName().toLowerCase().endsWith(".pdf")){
  231. throw new IOException("并非 pdf 文件:" + p);
  232. }
  233. fileService.rangeDownloadWithContentType(file, "application/pdf", request, response);
  234. }
  235. }