Forráskód Böngészése

loadPdfData改为pdfPath,并去除pageIndex参数、修改返回类型

sunyj 8 éve
szülő
commit
0ea2eec31c

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

@@ -23,6 +23,7 @@ import com.uas.report.core.exception.ReportException;
 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;
@@ -166,21 +167,10 @@ public class PrintController {
 			}
 			FileUtils.write(file.getPath(), data);
 		} else {
-			FileInputStream fileInputStream = null;
 			try {
-				fileInputStream = new FileInputStream(file);
-				data = new byte[fileInputStream.available()];
-				fileInputStream.read(data);
+				data = FileUtils.readData(new FileInputStream(file));
 			} catch (IOException e) {
 				throw new ReportException(e).setDetailedMessage(e);
-			} finally {
-				if (fileInputStream != null) {
-					try {
-						fileInputStream.close();
-					} catch (IOException e) {
-						throw new ReportException(e).setDetailedMessage(e);
-					}
-				}
 			}
 		}
 
@@ -206,7 +196,7 @@ public class PrintController {
 	}
 
 	/**
-	 * 报表预览时获取pdf
+	 * 报表预览时获取pdf相对路径
 	 * 
 	 * @param userName
 	 *            不为null;当前账套名称
@@ -219,61 +209,42 @@ public class PrintController {
 	 * @param otherParameters
 	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
 	 *            JSON格式,数据为键值对
-	 * @param pageIndex
-	 *            分页展示,当前页码,从0开始
 	 * @param flush
 	 *            是否强制刷新pdf、xls
 	 * @param request
 	 * @param response
-	 * @return 包括pageSize、pdfPath
+	 * @return pdf相对路径
 	 */
-	@RequestMapping(value = "/loadPdfData")
+	@RequestMapping(value = "/pdfPath")
 	@ResponseBody
-	public Map<String, Object> loadPdfData(String userName, final String profile, final String reportName,
-			final String whereCondition, final String otherParameters, Integer pageIndex, Boolean flush,
-			HttpServletRequest request, HttpServletResponse response) {
+	public String pdfPath(String userName, final String profile, final String reportName,
+			final String whereCondition, final String otherParameters, Boolean flush, HttpServletRequest request,
+			HttpServletResponse response) {
 		userName = userName == null ? null : userName.toUpperCase();
 		ReportUtils.checkParameters(userName, reportName);
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
-		Map<String, Object> result = new HashMap<>();
 
 		// 相对路径,返回给前端
 		String pdfPath = ReportConstants.GENERATED_FILES_PATH + reportName + "/" + fileService
 				.generateFileName(userName, profile, whereCondition, otherParameters, ReportConstants.PDF_FILE_TYPE)
 				+ "." + ReportConstants.PDF_FILE_TYPE;
 		final File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
-		if (pageIndex == null || pageIndex == 1) {
-			// 指定flush为true(强制刷新pdf)
-			// 文件无效(不存在或过期),重新创建pdf文件
-			if ((flush != null && flush.booleanValue()) || !fileService.isFileValid(file.getPath(),
-					fileService.getJrxmlFilePath(masterOfJrxml, reportName))) {
-				if (printService.overload(userName, profile, reportName, whereCondition, otherParameters,
-						Platform.PC)) {
-					throw new ReportException("数据量过大,无法提供服务");
-				}
-				// 参数pageIndex为null或1,表示是直接打印或预览第一页,
-				// 需要生成第一页(可能页数过多,提示用户不支持预览打印),再在后台开线程生成总的pdf
-				// 先生成第一页pdf
-				Integer pageSize = printService.createPdfFile(userName, profile, reportName, whereCondition,
-						otherParameters, file.getPath(), null);
-				result.put("pageSize", pageSize);
-			} else {
-				result.put("pageSize", fileService.getPageSize(file.getPath()));
+		// 指定flush为true(强制刷新pdf)
+		// 文件无效(不存在或过期),重新创建pdf文件
+		if ((flush != null && flush.booleanValue())
+				|| !fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(masterOfJrxml, reportName))) {
+			if (printService.overload(userName, profile, reportName, whereCondition, otherParameters, Platform.PC)) {
+				throw new ReportException("数据量过大,无法提供服务");
 			}
-		} else {
-			// 文件无效(不存在或过期),重新创建pdf文件
-			if (!fileService.isFileValid(
-					file.getPath().replace("." + ReportConstants.PDF_FILE_TYPE,
-							"_" + pageIndex + "." + ReportConstants.PDF_FILE_TYPE),
-					fileService.getJrxmlFilePath(masterOfJrxml, reportName))) {
-				printService.createPdfFile(userName, profile, reportName, whereCondition, otherParameters,
-						file.getPath().replace("." + ReportConstants.PDF_FILE_TYPE,
-								"_" + pageIndex + "." + ReportConstants.PDF_FILE_TYPE),
-						pageIndex);
+			Map<String, Object> result = printService.preview(userName, profile, reportName, whereCondition,
+					otherParameters, null);
+			if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
+				throw new ReportException("报表预览失败:" + userName + "/" + reportName + "\n");
 			}
+			byte[] data = (byte[]) result.remove("data");
+			FileUtils.write(file.getPath(), data);
 		}
-		result.put("pdfPath", pdfPath);
-		return result;
+		return pdfPath;
 	}
 
 	/**

+ 3 - 3
src/main/webapp/resources/js/preview/app.js

@@ -173,15 +173,15 @@ function getWindowWidth() {
  */
 function loadData() {
 	spinner = showLoading(spinner, spinnerContainer);
-	var loadPdfDataUrl = "print/loadPdfData" + window.location.search
+	var loadPdfDataUrl = "print/pdfPath" + window.location.search
 			+ "&flush=true";
 	$.ajax({
 		type : "get",
 		async : false,
 		url : loadPdfDataUrl,
-		success : function(data) {
+		success : function(path) {
 			document.title = getParameter("reportName");
-			pdfPath = data.pdfPath;
+			pdfPath = path;
 			hiddenFrame.src = pdfPath;
 			PDFJS.getDocument(pdfPath).then(function(pdfDoc_) {
 				pdfDoc = pdfDoc_;