Browse Source

预览时先生成pdf文件,再返回pdf文件路径

sunyj 9 years ago
parent
commit
70dfcb32c8

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

@@ -7,7 +7,6 @@ import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -143,27 +142,9 @@ public class PrintController {
 			logger.error(message);
 			throw new SystemError(message);
 		}
-		// 对pdf流进行base64编码
-		result.put("data", Base64.encodeBase64String(data));
+		String pdfPath = printService.writePdfFile(reportName, data);
+		result.put("pdfPath", pdfPath);
 		logger.info("预览报表成功:" + reportName);
-		OutputStream outputStream = null;
-		try {
-			outputStream = response.getOutputStream();
-			outputStream.write(data);
-			outputStream.flush();
-		} catch (IOException e) {
-			e.printStackTrace();
-			throw new SystemError(e.getMessage());
-		} finally {
-			if (outputStream != null) {
-				try {
-					outputStream.close();
-				} catch (IOException e) {
-					e.printStackTrace();
-					throw new SystemError(e.getMessage());
-				}
-			}
-		}
 		return result;
 	}
 }

+ 11 - 0
src/main/java/com/uas/report/service/PrintService.java

@@ -47,4 +47,15 @@ public interface PrintService {
 	 */
 	public Map<String, Object> preview(String userName, String reportName, String whereCondition,
 			String otherParameters, Integer pageIndex);
+
+	/**
+	 * 预览 时将pdf文件先写入本地,再将其路径返回给前台
+	 * 
+	 * @param reportName
+	 *            报表名称
+	 * @param data
+	 *            pdf文件的数据
+	 * @return pdf文件的路径
+	 */
+	public String writePdfFile(String reportName, byte[] data);
 }

+ 25 - 1
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -2,6 +2,7 @@ package com.uas.report.service.impl;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.URISyntaxException;
@@ -9,6 +10,7 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -29,6 +31,7 @@ import com.uas.report.service.PrintService;
 import com.uas.report.service.ResourceService;
 import com.uas.report.support.JasperserverRestAPIConf;
 import com.uas.report.util.ContextUtils;
+import com.uas.report.util.PathUtils;
 import com.uas.report.util.ReportConstants;
 
 import net.sf.jasperreports.engine.JRException;
@@ -80,6 +83,27 @@ public class PrintServiceImpl implements PrintService {
 		return print(userName, reportName, whereCondition, otherParameters, null, pageIndex);
 	}
 
+	@Override
+	public String writePdfFile(String reportName, byte[] data) {
+		String pdfFilePath = "resources/generate/pdf/" + reportName + "_" + new Date().getTime() + ".pdf";
+		File pdfFile = new File(PathUtils.getAppPath() + pdfFilePath);
+
+		if (!pdfFile.getParentFile().exists()) {
+			pdfFile.getParentFile().mkdirs();
+		}
+		try {
+			FileOutputStream fos = new FileOutputStream(pdfFile);
+			fos.write(data);
+			fos.flush();
+			logger.info("Generated pdf file: " + pdfFile.getPath());
+			fos.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+			throw new SystemError(e.getMessage());
+		}
+		return pdfFilePath;
+	}
+
 	/**
 	 * 输出报表的数据
 	 * 
@@ -341,7 +365,7 @@ public class PrintServiceImpl implements PrintService {
 		} catch (JRException e) {
 			e.printStackTrace();
 			throw new SystemError(e.getMessage());
-//			return false;
+			// return false;
 		}
 		return true;
 	}

+ 9 - 11
src/main/webapp/resources/js/preview/app.js

@@ -67,17 +67,15 @@ function loadData() {
 		pageIndex = 1;
 		loadDataUrl = loadDataUrl + "&pageIndex=" + pageIndex;
 	}
-	DEFAULT_URL = loadDataUrl;
-
-	/*
-	 * $.ajax({ type : "post", async : false, url : "print/loadPdfData", data :
-	 * getParameters(), success : function(data) { // pageSize = data.pageSize;
-	 * PDFData = base64_decode(data); // PDFData = data; var rawLength =
-	 * PDFData.length; // // 转换成pdf.js能直接解析的Uint8Array类型,见pdf.js-4068 var array =
-	 * new Uint8Array(new ArrayBuffer(rawLength)); for (var i = 0; i <
-	 * rawLength; i++) { array[i] = PDFData.charCodeAt(i) & 0xff; } DEFAULT_URL =
-	 * array; } });
-	 */
+	$.ajax({
+		type : "get",
+		async : false,
+		url : loadDataUrl,
+		success : function(data) {
+			DEFAULT_URL = data.pdfPath;
+		}
+	});
+
 };
 
 // base64 解码