sunyj 8 жил өмнө
parent
commit
befe522517

+ 115 - 0
src/main/java/com/uas/report/controller/PdfController.java

@@ -0,0 +1,115 @@
+package com.uas.report.controller;
+
+import java.io.File;
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.dom4j.DocumentException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import com.uas.report.service.FileService;
+import com.uas.report.service.PrintService;
+import com.uas.report.util.ArrayUtils;
+import com.uas.report.util.CollectionUtils;
+import com.uas.report.util.FileUtils;
+import com.uas.report.util.Platform;
+import com.uas.report.util.ReportConstants;
+import com.uas.report.util.ReportUtils;
+
+import net.sf.jasperreports.engine.JRException;
+
+/**
+ * 获取pdf
+ * 
+ * @author sunyj
+ * @since 2017年8月10日 下午7:17:38
+ */
+@Controller
+@RequestMapping("/pdf")
+public class PdfController {
+
+	@Autowired
+	private PrintService printService;
+
+	@Autowired
+	private FileService fileService;
+
+	/**
+	 * @param u
+	 *            不为null;当前账套名称
+	 * @param p
+	 *            可选(UAS等系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
+	 * @param r
+	 *            不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
+	 * @param w
+	 *            可为null;where之后的条件(包括where)
+	 * @param o
+	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
+	 *            JSON格式,数据为键值对
+	 * @param request
+	 * @param response
+	 * @return JSON格式
+	 * 
+	 *         <table border=1 cellpadding=5 cellspacing=0 summary=
+	 *         "result">
+	 *         <tr>
+	 *         <th>key</th>
+	 *         <th>description</th>
+	 *         </tr>
+	 *         <tr>
+	 *         <td>pdf</td>
+	 *         <td>pdf相对路径</td>
+	 *         </tr>
+	 *         <tr>
+	 *         <td>pageSize</td>
+	 *         <td>pdf页数</td>
+	 *         </tr>
+	 *         <tr>
+	 *         <td>overload</td>
+	 *         <td>数据量是否过大</td>
+	 *         </tr>
+	 *         </table>
+	 * @throws JRException
+	 * @throws IOException
+	 * @throws DocumentException
+	 * @throws SQLException
+	 */
+	@RequestMapping(value = "/path")
+	@ResponseBody
+	public Map<String, Object> getPath(String u, String p, String r, String w, String o, HttpServletRequest request,
+			HttpServletResponse response) throws JRException, IOException, DocumentException, SQLException {
+		u = u == null ? null : u.toUpperCase();
+		ReportUtils.checkParameters(u, r);
+		Map<String, Object> result = new HashMap<>();
+		// 判断是否过载
+		if (printService.overload(u, p, r, w, o, Platform.PHONE)) {
+			result.put("path", "");
+			result.put("pageSize", 0);
+			result.put("overload", true);
+		} else {
+			result = printService.preview(u, p, r, w, o, null);
+			if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
+				throw new IllegalStateException("pdf生成失败:" + u + "/" + r);
+			}
+			byte[] data = (byte[]) result.remove("data");
+			// 相对路径
+			String pdfPath = ReportConstants.GENERATED_FILES_PATH + r + "/"
+					+ fileService.generateFileName(u, p, w, o, ReportConstants.FILE_TYPE_PDF) + "."
+					+ ReportConstants.FILE_TYPE_PDF;
+			File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
+			FileUtils.write(file.getPath(), data);
+			result.put("path", pdfPath);
+			result.put("overload", false);
+		}
+		return result;
+	}
+
+}

+ 5 - 5
src/main/java/com/uas/report/controller/PrintController.java

@@ -226,9 +226,9 @@ public class PrintController {
 	 */
 	 */
 	@RequestMapping(value = "/pdfPath")
 	@RequestMapping(value = "/pdfPath")
 	@ResponseBody
 	@ResponseBody
-	public String pdfPath(String userName, final String profile, final String reportName, final String whereCondition,
-			final String otherParameters, Boolean flush, HttpServletRequest request, HttpServletResponse response)
-			throws JRException, IOException, DocumentException, SQLException {
+	public String getPdfPath(String userName, final String profile, final String reportName,
+			final String whereCondition, final String otherParameters, Boolean flush, HttpServletRequest request,
+			HttpServletResponse response) throws JRException, IOException, DocumentException, SQLException {
 		userName = userName == null ? null : userName.toUpperCase();
 		userName = userName == null ? null : userName.toUpperCase();
 		ReportUtils.checkParameters(userName, reportName);
 		ReportUtils.checkParameters(userName, reportName);
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
@@ -237,7 +237,7 @@ public class PrintController {
 		String pdfPath = ReportConstants.GENERATED_FILES_PATH + reportName + "/" + fileService
 		String pdfPath = ReportConstants.GENERATED_FILES_PATH + reportName + "/" + fileService
 				.generateFileName(userName, profile, whereCondition, otherParameters, ReportConstants.FILE_TYPE_PDF)
 				.generateFileName(userName, profile, whereCondition, otherParameters, ReportConstants.FILE_TYPE_PDF)
 				+ "." + ReportConstants.FILE_TYPE_PDF;
 				+ "." + ReportConstants.FILE_TYPE_PDF;
-		final File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
+		File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
 		// 指定flush为true(强制刷新pdf)
 		// 指定flush为true(强制刷新pdf)
 		// 文件无效(不存在或过期),重新创建pdf文件
 		// 文件无效(不存在或过期),重新创建pdf文件
 		if ((flush != null && flush.booleanValue())
 		if ((flush != null && flush.booleanValue())
@@ -248,7 +248,7 @@ public class PrintController {
 			Map<String, Object> result = printService.preview(userName, profile, reportName, whereCondition,
 			Map<String, Object> result = printService.preview(userName, profile, reportName, whereCondition,
 					otherParameters, null);
 					otherParameters, null);
 			if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
 			if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
-				throw new IllegalStateException("报表预览失败:" + userName + "/" + reportName + "\n");
+				throw new IllegalStateException("报表预览失败:" + userName + "/" + reportName);
 			}
 			}
 			byte[] data = (byte[]) result.remove("data");
 			byte[] data = (byte[]) result.remove("data");
 			FileUtils.write(file.getPath(), data);
 			FileUtils.write(file.getPath(), data);