Browse Source

打印后返回字节数组,不再直接写入response的输出流中;增加error.jsp用于展示打印的结果

sunyj 9 years ago
parent
commit
da55f6f94a

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

@@ -1,6 +1,5 @@
 package com.uas.report.controller;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.HashMap;
@@ -9,12 +8,12 @@ import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
 
 import com.uas.report.service.PrintService;
 
@@ -51,19 +50,20 @@ public class PrintController {
 	 * @return
 	 */
 	@RequestMapping
-	@ResponseBody
 	public String print(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
-			String exportFileType, HttpServletResponse response) {
+			String exportFileType, HttpServletRequest request, HttpServletResponse response) {
 		String message = "";
 		if (StringUtils.isEmpty(userName)) {
 			message = "未传入当前账套用户名!";
 			logger.error(message);
-			return message;
+			request.setAttribute("message", message);
+			return "error.jsp";
 		}
 		if (StringUtils.isEmpty(reportName)) {
 			message = "报表名称无效!";
 			logger.error(message);
-			return message;
+			request.setAttribute("message", message);
+			return "error.jsp";
 		}
 
 		logger.info("开始打印报表: " + reportName);
@@ -71,10 +71,18 @@ public class PrintController {
 			exportFileType = "pdf";
 		}
 
+		byte[] data = printService.print(userName, reportName, whereCondition, otherParameters, exportFileType);
+		if (ArrayUtils.isEmpty(data)) {
+			message = "报表 " + reportName + " 打印失败!";
+			logger.error(message);
+			request.setAttribute("message", message);
+			return "error.jsp";
+		}
+
 		try {
 			response.setHeader("Content-Disposition", "attachment;filename=" + reportName + "." + exportFileType);
 			OutputStream outputStream = response.getOutputStream();
-			printService.print(userName, reportName, whereCondition, otherParameters, exportFileType, outputStream);
+			outputStream.write(data);
 			outputStream.flush();
 			outputStream.close();
 		} catch (IOException e) {
@@ -83,7 +91,7 @@ public class PrintController {
 
 		message = "报表 " + reportName + " 打印完成!";
 		logger.info(message);
-		return message;
+		return null;
 	}
 
 	/**
@@ -93,8 +101,7 @@ public class PrintController {
 	 * @return
 	 */
 	@RequestMapping("/test")
-	@ResponseBody
-	public String testPrint(HttpServletResponse response) {
+	public String testPrint(HttpServletRequest request, HttpServletResponse response) {
 		String reportName = "Purchase";
 		// String userName = "UAS";
 		// String whereCondition = "where pu_code = 'MP160800017' and pd_qty >
@@ -105,7 +112,7 @@ public class PrintController {
 		Map<String, Object> otherParameters = new HashMap<>();
 		otherParameters.put("OTHER_PARAMETER_TEST", "天气真好!");
 		String exportFileType = "xls";
-		return print(userName, reportName, whereCondition, otherParameters, exportFileType, response);
+		return print(userName, reportName, whereCondition, otherParameters, exportFileType, request, response);
 	}
 
 	/**
@@ -130,22 +137,29 @@ public class PrintController {
 		if (StringUtils.isEmpty(userName)) {
 			message = "未传入当前账套用户名!";
 			logger.error(message);
-			return message;
+			request.setAttribute("message", message);
+			return "error.jsp";
 		}
 		if (StringUtils.isEmpty(reportName)) {
 			message = "报表名称无效!";
 			logger.error(message);
-			return message;
+			request.setAttribute("message", message);
+			return "error.jsp";
 		}
 
 		logger.info("开始打印报表: " + reportName);
 		String exportFileType = "pdf";
-		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-		printService.print(userName, reportName, whereCondition, otherParameters, exportFileType, outputStream);
-		request.setAttribute("data", outputStream.toByteArray());
+		byte[] data = printService.print(userName, reportName, whereCondition, otherParameters, exportFileType);
+		if (ArrayUtils.isEmpty(data)) {
+			message = "报表 " + reportName + " 预览失败!";
+			logger.error(message);
+			request.setAttribute("message", message);
+			return "error.jsp";
+		}
+		request.setAttribute("data", data);
 		request.setAttribute("message", "test");
-		message = "报表 " + reportName + " 打印完成!";
+		message = "报表 " + reportName + " 预览完成!";
 		logger.info(message);
-		return "preview";
+		return "preview.jsp";
 	}
 }

+ 3 - 5
src/main/java/com/uas/report/service/PrintService.java

@@ -1,6 +1,5 @@
 package com.uas.report.service;
 
-import java.io.OutputStream;
 import java.util.Map;
 
 /**
@@ -24,9 +23,8 @@ public interface PrintService {
 	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数
 	 * @param exportFileType
 	 *            报表打印的格式,默认为pdf
-	 * @param outputStream
-	 *            输出流
+	 * @return 打印的文件的字节数组
 	 */
-	public void print(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
-			String exportFileType, OutputStream outputStream);
+	public byte[] print(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
+			String exportFileType);
 }

+ 28 - 10
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -1,6 +1,8 @@
 package com.uas.report.service.impl;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
 import java.io.OutputStream;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
@@ -48,10 +50,10 @@ public class PrintServiceImpl implements PrintService {
 	private static Logger logger = Logger.getLogger(PrintService.class);
 
 	@Override
-	public void print(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
-			String exportFileType, OutputStream outputStream) {
+	public byte[] print(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
+			String exportFileType) {
 		if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
-			return;
+			return null;
 		}
 
 		// 报表路径为报表根路径REPORT_DIR + 当前账套用户名userName
@@ -64,7 +66,7 @@ public class PrintServiceImpl implements PrintService {
 		// 报表模板不存在
 		if (!jrxmlFile.exists()) {
 			logger.error("未发现模板文件: " + jrxmlFile.getPath());
-			return;
+			return null;
 		}
 
 		String jasperFilePath = jrxmlFile.getPath().replace(".jrxml", ".jasper");
@@ -98,26 +100,39 @@ public class PrintServiceImpl implements PrintService {
 			parameters.putAll(otherParameters);
 		}
 
+		Connection connection = null;
 		try {
 			// 获取数据源
 			DataSource dataSource = getDataSource(userName);
 			if (dataSource == null) {
-				return;
+				return null;
 			}
-			Connection connection = dataSource.getConnection();
+			connection = dataSource.getConnection();
 
 			// 从数据库获取数据填充报表
 			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
-			boolean exportCompleted = exportReport(jasperPrint, exportFileType, outputStream);
-			if (!exportCompleted) {
-				logger.error("报表打印失败!");
+			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+			if (exportReport(jasperPrint, exportFileType, outputStream)) {
+				byte[] data = outputStream.toByteArray();
+				outputStream.close();
+				return data;
 			}
-			connection.close();
 		} catch (SQLException e) {
 			logger.error("数据库连接错误!");
 		} catch (JRException e) {
 			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			if (connection != null) {
+				try {
+					connection.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
+			}
 		}
+		return null;
 	}
 
 	/**
@@ -205,6 +220,9 @@ public class PrintServiceImpl implements PrintService {
 				exportReportToPdf(jasperPrint, outputStream);
 			} else if (exportFileType.equals("xls")) {
 				exportReportToXls(jasperPrint, outputStream);
+			} else {
+				logger.error("不支持" + exportFileType + "格式!");
+				return false;
 			}
 		} catch (JRException e) {
 			e.printStackTrace();

+ 10 - 0
src/main/webapp/WEB-INF/views/error.jsp

@@ -0,0 +1,10 @@
+<%@ page language="java" contentType="text/html; UTF-8"
+	pageEncoding="UTF-8"%>
+<html>
+<body>
+	<%
+		String message = (String) request.getAttribute("message");
+	%>
+	<p><%=message%></p>
+</body>
+</html>

+ 1 - 2
src/main/webapp/WEB-INF/webmvc.xml

@@ -23,10 +23,9 @@
 	<bean
 		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 		<property name="prefix" value="/WEB-INF/views/" />
-		<property name="suffix" value=".jsp" />
 		<property name="contentType" value="text/html;charset=UTF-8" />
 	</bean>
 
-	<mvc:view-controller path="/" view-name="index" />
+	<mvc:view-controller path="/" view-name="index.jsp" />
 
 </beans>