Browse Source

采用Logger;修改报表路径;修改报表打印测试

sunyj 9 years ago
parent
commit
f6f3c9f1f5

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

@@ -7,6 +7,8 @@ import java.util.Map;
 
 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;
@@ -26,6 +28,8 @@ import com.uas.report.service.PrintService;
 @RequestMapping("/print")
 public class PrintController {
 
+	private static Logger logger = Logger.getLogger(PrintController.class);
+
 	@Autowired
 	private PrintService printService;
 
@@ -48,14 +52,15 @@ public class PrintController {
 		String message = "";
 		if (StringUtils.isEmpty(reportName)) {
 			message = "报表名称无效!";
-			System.out.println(message);
+			logger.error(message);
 			return message;
 		}
 
-		System.out.println("\n开始打印报表: " + reportName + "...");
+		logger.info("开始打印报表: " + reportName);
 		byte[] results = printService.print(reportName, whereCondition, otherParameters);
-		if (results == null || results.length < 1) {
+		if (ArrayUtils.isEmpty(results)) {
 			message = "报表 " + reportName + " 打印失败!";
+			logger.error(message);
 			return message;
 		}
 
@@ -67,49 +72,28 @@ public class PrintController {
 			outputStream.flush();
 			outputStream.close();
 		} catch (IOException e) {
-			System.out.println("连接被关闭!");
+			logger.error("连接被关闭!");
+			e.printStackTrace();
 		}
 
 		message = "报表 " + reportName + " 打印完成!";
-		System.out.println(message);
+		logger.info(message);
 		return message;
 	}
 
+	/**
+	 * 测试打印
+	 * 
+	 * @param response
+	 * @return
+	 */
 	@RequestMapping("/test")
 	@ResponseBody
 	public String testPrint(HttpServletResponse response) {
 		String reportName = "Purchase";
-		String whereCondition = "where pu_code = 'MP160800017'";
+		String whereCondition = "where pu_code = 'MP160800017' and pd_qty > 1000";
 		Map<String, Object> otherParameters = new HashMap<>();
 		otherParameters.put("OTHER_PARAMETER_1", "天气真好!");
-
-		String message = "";
-		if (StringUtils.isEmpty(reportName)) {
-			message = "报表名称无效!";
-			System.out.println(message);
-			return message;
-		}
-
-		System.out.println("\n开始打印报表: " + reportName + "...");
-		byte[] results = printService.print(reportName, whereCondition, otherParameters);
-		if (results == null || results.length < 1) {
-			message = "报表 " + reportName + " 打印失败!";
-			return message;
-		}
-
-		OutputStream outputStream = null;
-		try {
-			response.setContentType("application/pdf");
-			outputStream = response.getOutputStream();
-			outputStream.write(results);
-			outputStream.flush();
-			outputStream.close();
-		} catch (IOException e) {
-			System.out.println("连接被关闭!");
-		}
-
-		message = "报表 " + reportName + " 打印完成!";
-		System.out.println(message);
-		return message;
+		return print(reportName, whereCondition, otherParameters, response);
 	}
 }

+ 9 - 5
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -7,6 +7,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.log4j.Logger;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
@@ -30,6 +31,8 @@ import net.sf.jasperreports.engine.xml.JRXmlLoader;
 @Service
 public class PrintServiceImpl implements PrintService {
 
+	private static Logger logger = Logger.getLogger(PrintService.class);
+
 	@Override
 	public byte[] print(String reportName, String whereCondition, Map<String, Object> otherParameters) {
 		byte[] results = null;
@@ -37,25 +40,26 @@ public class PrintServiceImpl implements PrintService {
 			return results;
 		}
 
-		File jrxmlFile = new File(ReportConstants.REPORT_DIR + reportName + ".jrxml");
+		File jrxmlFile = new File(ReportConstants.REPORT_DIR + "jrxml" + File.separator + reportName + ".jrxml");
 		// 报表模板不存在
 		if (!jrxmlFile.exists()) {
-			System.out.println(jrxmlFile.getPath() + " not found!");
+			logger.error("未发现模板文件: " + jrxmlFile.getPath());
 			return results;
 		}
 
-		String jasperFilePath = ReportConstants.REPORT_DIR + reportName + ".jasper";
+		String jasperFilePath = jrxmlFile.getPath().replace(".jrxml", ".jasper");
 		// 报表模板编译后的文件
 		File jasperFile = new File(jasperFilePath);
 		try {
 			// 报表模板未编译过
 			if (!jasperFile.exists()) {
+				logger.info("正在编译报表模板...");
 				JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
 				JasperCompileManager.compileReportToFile(jasperDesign, jasperFilePath);
 			} else {
 				// 如果在编译之后,报表模板有更改过 ,重新编译
 				if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
-					System.out.println("重新编译报表模板...");
+					logger.info("正在重新编译报表模板...");
 					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
 					JasperCompileManager.compileReportToFile(jasperDesign, jasperFilePath);
 				}
@@ -78,7 +82,7 @@ public class PrintServiceImpl implements PrintService {
 			results = JasperRunManager.runReportToPdf(jasperFilePath, parameters, connection);
 			connection.close();
 		} catch (SQLException e) {
-			e.printStackTrace();
+			logger.error("数据库连接错误!");
 		} catch (JRException e) {
 			e.printStackTrace();
 		}

+ 3 - 3
src/main/java/com/uas/report/util/ReportConstants.java

@@ -10,9 +10,9 @@ import java.io.File;
  */
 public class ReportConstants {
 	/**
-	 * 报表模板路径
+	 * 报表路径
 	 */
-	public static final String REPORT_DIR = PathUtils.getFilePath() + "jrxml" + File.separator;
+	public static final String REPORT_DIR = PathUtils.getFilePath() + "reports" + File.separator;
 
 	/**
 	 * 报表参数 - WHERE_CONDITION,where字句(需含where)
@@ -20,7 +20,7 @@ public class ReportConstants {
 	public static final String PARAMETER_WHERE_CONDITION = "WHERE_CONDITION";
 
 	/**
-	 * 报表参数 - REPORT_DIR,报表模板的路径
+	 * 报表参数 - REPORT_DIR,报表的路径
 	 */
 	public static final String PARAMETER_REPORT_DIR = "REPORT_DIR";
 }