Просмотр исходного кода

添加日志输出,以便跟踪程序运行状况,找到导致预览有时出现卡顿的原因

sunyj 9 лет назад
Родитель
Сommit
232eae3895

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

@@ -24,6 +24,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.FileUtils;
+import com.uas.report.util.IpHelper;
 import com.uas.report.util.PathUtils;
 import com.uas.report.util.ReportConstants;
 import com.uas.report.util.ReportUtils;
@@ -70,6 +71,9 @@ public class PrintController {
 	@RequestMapping("")
 	public void print(String userName, String profile, String reportName, String whereCondition, String otherParameters,
 			String printType, HttpServletRequest request, HttpServletResponse response) {
+		logger.info(new StringBuilder("ip=").append(IpHelper.getIp(request)).append(", userName=").append(userName)
+				.append(", reportName=").append(reportName).append(", whereCondition=").append(whereCondition)
+				.append(", otherParameters=").append(otherParameters));
 		// printType为空,默认进入预览页
 		if (StringUtils.isEmpty(printType)) {
 			printType = ReportConstants.PREVIEW_PRINT_TYPE;
@@ -123,7 +127,7 @@ public class PrintController {
 			String otherParameters, String exportFileType, HttpServletResponse response) {
 		ReportUtils.checkParameters(userName, reportName);
 
-		logger.info("开始导出报表:" + reportName);
+		logger.info("开始导出报表:" + userName + "/" + reportName);
 		if (StringUtils.isEmpty(exportFileType)) {
 			exportFileType = ReportConstants.PDF_FILE_TYPE;
 		}
@@ -142,7 +146,7 @@ public class PrintController {
 		if (!fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName))) {
 			data = printService.export(userName, profile, reportName, whereCondition, otherParameters, exportFileType);
 			if (ArrayUtils.isEmpty(data)) {
-				throw new ReportException("报表导出失败:" + reportName);
+				throw new ReportException("报表导出失败:" + userName + "/" + reportName);
 			}
 			FileUtils.create(file.getPath(), data);
 		} else {
@@ -180,7 +184,7 @@ public class PrintController {
 		} catch (IOException e) {
 			logger.error("浏览器重复请求!");
 		}
-		logger.info("报表导出完成:" + reportName);
+		logger.info("报表导出完成:" + userName + "/" + reportName + "\n");
 	}
 
 	/**
@@ -208,7 +212,7 @@ public class PrintController {
 			final String whereCondition, final String otherParameters, Integer pageIndex,
 			HttpServletResponse response) {
 		ReportUtils.checkParameters(userName, reportName);
-		logger.info("开始预览报表:" + reportName);
+		logger.info("开始预览报表:" + userName + "/" + reportName);
 		Map<String, Object> result = new HashMap<>();
 
 		// 相对路径,返回给前端
@@ -254,7 +258,7 @@ public class PrintController {
 			}
 		}
 		result.put("pdfPath", pdfPath);
-		logger.info("预览报表成功:" + reportName);
+		logger.info("预览报表成功:" + userName + "/" + reportName + "\n");
 		return result;
 	}
 

+ 17 - 15
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -89,17 +89,17 @@ public class PrintServiceImpl implements PrintService {
 			String otherParameters, Integer pageIndex) {
 		DataSource dataSource = null;
 		if (!StringUtils.isEmpty(profile)) {
-			logger.info("point 6");
+			logger.info("getPlatformDataSource...");
 			dataSource = DataSourceUtils.getPlatformDataSource(userName, profile);
 		} else {
-			logger.info("point 7");
+			logger.info("getUASDataSource...");
 			// 为空,说明不是来自B2C或B2B的请求,而是UAS系统
 			dataSource = DataSourceUtils.getUASDataSource(userName);
 		}
 		if (dataSource == null) {
 			throw new ReportException("获取数据源失败");
 		}
-		logger.info("point 8");
+		logger.info("dataSource getted...");
 		return print(userName, reportName, whereCondition, otherParameters, null, pageIndex, dataSource);
 	}
 
@@ -140,17 +140,18 @@ public class PrintServiceImpl implements PrintService {
 
 		Connection connection = null;
 		try {
-			logger.info("point 13");
+			logger.info("dataSource.getConnection...");
 			connection = dataSource.getConnection();
-			logger.info("point 14");
-			Map<String, Object> result = new HashMap<>();
+			logger.info("dataSource.getConnection done...");
 
+			Map<String, Object> result = new HashMap<>();
+			byte[] data = null;
 			// 从数据库获取数据填充报表
 			JasperPrint jasperPrint = null;
 			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-
 			// exportFileType导出文件的格式不为空,表示是导出,并非预览
 			if (!StringUtils.isEmpty(exportFileType)) {
+				logger.info("export fillReport...");
 				// 只导出数据
 				if (exportFileType.equals("xls_with_only_data")) {
 					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
@@ -165,13 +166,13 @@ public class PrintServiceImpl implements PrintService {
 				}
 
 				exportReport(jasperPrint, exportFileType, outputStream);
-				byte[] data = outputStream.toByteArray();
+				logger.info("export fillReport done...");
+				data = outputStream.toByteArray();
 				outputStream.close();
-				result.put("data", data);
-				return result;
 			}
 			// 报表预览,则直接输出pdf,并且需要进行分页
 			else {
+				logger.info("preview fillReport...");
 				jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
 				JRPdfExporter exporter = new JRPdfExporter();
 				if (pageIndex != null) {
@@ -191,20 +192,21 @@ public class PrintServiceImpl implements PrintService {
 				exporter.setExporterOutput(exporterOutput);
 				exporter.exportReport();
 
-				byte[] data = outputStream.toByteArray();
+				logger.info("preview fillReport done...");
+				data = outputStream.toByteArray();
 				outputStream.close();
-				result.put("data", data);
 				result.put("pageSize", jasperPrint.getPages().size());
-				return result;
 			}
+			result.put("data", data);
+			return result;
 		} catch (SQLException | JRException | IOException e) {
 			throw new ReportException(e).setDetailedMessage(e);
 		} finally {
 			if (connection != null) {
 				try {
-					logger.info("point 21");
+					logger.info("connection.close...");
 					connection.close();
-					logger.info("point 22");
+					logger.info("connection.close done...");
 				} catch (SQLException e) {
 					throw new ReportException(e).setDetailedMessage(e);
 				}

+ 1 - 1
src/main/java/com/uas/report/util/ChineseMoneyUtils.java

@@ -3,7 +3,7 @@ package com.uas.report.util;
 import java.math.BigDecimal;
 
 /**
- * 数字转为中文大写金额
+ * 数字转为中文大写金额(用于UAS系统的模板制作)
  * 
  * @author sunyj
  * @since 2016年9月27日 下午8:11:04

+ 6 - 0
src/main/java/com/uas/report/util/DataSourceUtils.java

@@ -8,6 +8,7 @@ import java.util.Properties;
 
 import javax.sql.DataSource;
 
+import org.apache.log4j.Logger;
 import org.springframework.util.StringUtils;
 
 import com.alibaba.druid.pool.DruidDataSource;
@@ -22,6 +23,8 @@ import com.uas.report.core.exception.ReportException;
  */
 public class DataSourceUtils {
 
+	private static Logger logger = Logger.getLogger(DataSourceUtils.class);
+
 	/**
 	 * 根据当前账套用户名,从主数据库master表获取(UAS系统)账套数据库配置信息,作为报表模板的数据源
 	 * 
@@ -48,7 +51,9 @@ public class DataSourceUtils {
 		PreparedStatement preparedStatement = null;
 		ResultSet resultSet = null;
 		try {
+			logger.info("defaultDataSource.getConnection...");
 			connection = defaultDataSource.getConnection();
+			logger.info("defaultDataSource.getConnection done...");
 			// 根据当前账套用户名获取其数据库配置信息
 			String sql = "select * from master where MA_USER = ?";
 			preparedStatement = connection.prepareStatement(sql);
@@ -64,6 +69,7 @@ public class DataSourceUtils {
 					properties.put("password", password);
 					properties.put("testWhileIdle", "true");
 					properties.put("validationQuery", "select 1 from SYS.DUAL");
+					logger.info("DruidDataSourceFactory.createDataSource...");
 					return DruidDataSourceFactory.createDataSource(properties);
 				}
 			}

+ 44 - 0
src/main/java/com/uas/report/util/IpHelper.java

@@ -0,0 +1,44 @@
+package com.uas.report.util;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * <p>
+ * 获取IP地址类
+ * </p>
+ * 
+ */
+public class IpHelper {
+
+	/**
+	 * 获取客户端IP
+	 * 
+	 * @param request
+	 * @return
+	 */
+	public static String getIp(HttpServletRequest request) {
+		String ipAddress = null;
+		ipAddress = request.getHeader("X-Forwarded-For");
+		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
+			ipAddress = request.getHeader("Proxy-Client-IP");
+		}
+		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
+			ipAddress = request.getHeader("WL-Proxy-Client-IP");
+		}
+		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
+			ipAddress = request.getRemoteAddr();
+		}
+		// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
+		if (ipAddress != null && ipAddress.length() > 15) {
+			if (ipAddress.indexOf(",") > 0) {
+				ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
+			}
+		}
+		// window7系统下,用localhost访问时,ip会变成0:0:0:0:0:0:0:1
+		if (ipAddress != null && "0:0:0:0:0:0:0:1".equals(ipAddress)) {
+			ipAddress = "127.0.0.1";
+		}
+		return ipAddress;
+	}
+
+}

+ 1 - 1
src/main/java/com/uas/report/util/TimeUtils.java

@@ -4,7 +4,7 @@ import java.util.Calendar;
 import java.util.Date;
 
 /**
- * 对时间的操作
+ * 对时间的操作(用于UAS系统的模板制作)
  * 
  * @author sunyj
  * @since 2016年9月27日 下午8:10:50

+ 0 - 1
src/main/webapp/WEB-INF/views/index.html

@@ -3,7 +3,6 @@
 <head>
 <meta charset="UTF-8">
 <title>Report</title>
-<link rel="stylesheet" href="static/css/index.css">
 </head>
 <body>
 	<p>Report Home</p>