Browse Source

生成的pdf文件有效期最高为5分钟,超过重新生成

sunyj 9 years ago
parent
commit
e991b1a76c

+ 11 - 1
src/main/java/com/uas/report/controller/PrintController.java

@@ -1,5 +1,6 @@
 package com.uas.report.controller;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Map;
@@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 
 import com.uas.report.core.exception.SystemError;
 import com.uas.report.service.PrintService;
+import com.uas.report.util.PathUtils;
 import com.uas.report.util.ReportConstants;
 
 /**
@@ -174,7 +176,15 @@ public class PrintController {
 			logger.error(message);
 			throw new SystemError(message);
 		}
-		String pdfPath = printService.writePdfFile(reportName, data);
+
+		String pdfPath = ReportConstants.GENERATED_FILES_PATH
+				+ printService.generateFileName(userName, reportName, whereCondition, otherParameters) + ".pdf";
+		File file = new File(PathUtils.getAppPath() + pdfPath);
+		// 文件过期或不存在,重新创建pdf文件
+		if (printService.isFileExpired(file)) {
+			printService.writePdfFile(file.getPath(), data);
+		}
+
 		result.put("pdfPath", pdfPath);
 		logger.info("预览报表成功:" + reportName);
 		return result;

+ 30 - 4
src/main/java/com/uas/report/service/PrintService.java

@@ -1,5 +1,6 @@
 package com.uas.report.service;
 
+import java.io.File;
 import java.util.Map;
 
 /**
@@ -51,11 +52,36 @@ public interface PrintService {
 	/**
 	 * 预览 时将pdf文件先写入本地,再将其路径返回给前台
 	 * 
-	 * @param reportName
-	 *            报表名称
+	 * @param absolutePath
+	 *            要写入的文件的绝对路径
 	 * @param data
 	 *            pdf文件的数据
-	 * @return pdf文件的路径
+	 * @return
+	 */
+	public void writePdfFile(String absolutePath, byte[] data);
+
+	/**
+	 * 根据报表名、账套名等生成文件名
+	 * 
+	 * @param userName
+	 *            不为null;当前账套用户名
+	 * @param reportName
+	 *            不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
+	 * @param whereCondition
+	 *            可为null;where之后的条件(包括where)
+	 * @param otherParameters
+	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
+	 *            JSON格式,数据为键值对
+	 * @return 生成的文件名(不含后缀名)
+	 */
+	public String generateFileName(String userName, String reportName, String whereCondition, String otherParameters);
+
+	/**
+	 * 判断文件是否过期(文件不存在视为过期)
+	 * 
+	 * @param file
+	 *            所指定的文件
+	 * @return 是否过期
 	 */
-	public String writePdfFile(String reportName, byte[] data);
+	public boolean isFileExpired(File file);
 }

+ 34 - 6
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -35,7 +35,6 @@ 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;
@@ -88,13 +87,12 @@ public class PrintServiceImpl implements PrintService {
 	}
 
 	@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);
-
+	public void writePdfFile(String absolutePath, byte[] data) {
+		File pdfFile = new File(absolutePath);
 		if (!pdfFile.getParentFile().exists()) {
 			pdfFile.getParentFile().mkdirs();
 		}
+
 		try {
 			FileOutputStream fos = new FileOutputStream(pdfFile);
 			fos.write(data);
@@ -107,7 +105,36 @@ public class PrintServiceImpl implements PrintService {
 			e.printStackTrace();
 			throw new SystemError(e.getMessage());
 		}
-		return pdfFilePath;
+	}
+
+	@Override
+	public String generateFileName(String userName, String reportName, String whereCondition, String otherParameters) {
+		if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
+			return null;
+		}
+		StringBuilder stringBuilder = new StringBuilder(userName);
+		if (!StringUtils.isEmpty(whereCondition)) {
+			stringBuilder.append(whereCondition);
+		}
+		if (!StringUtils.isEmpty(otherParameters)) {
+			stringBuilder.append(otherParameters);
+		}
+		// 文件名:reportName + hashCode
+		return reportName + "_" + stringBuilder.toString().hashCode();
+	}
+
+	@Override
+	public boolean isFileExpired(File file) {
+		if (file != null && file.exists()) {
+			long interval = new Date().getTime() - file.lastModified();
+			// 剩余的有效期(最高为5分钟)
+			long validity = 5 * 60 * 1000 - interval;
+			if (validity > 0) {
+				logger.info(file.getName() + " will be expired after " + validity / 1000.0 + "s");
+				return false;
+			}
+		}
+		return true;
 	}
 
 	/**
@@ -464,4 +491,5 @@ public class PrintServiceImpl implements PrintService {
 		// textField.setStretchWithOverflow(false);
 		// }
 	}
+
 }

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

@@ -25,4 +25,9 @@ public class ReportConstants {
 	public static final String PRINT_PRINT_TYPE = "PRINT";
 	public static final String PDF_PRINT_TYPE = "PDF";
 	public static final String EXCEL_PRINT_TYPE = "EXCEL";
+
+	/**
+	 * 生成的pdf、excel等文件的路径
+	 */
+	public static final String GENERATED_FILES_PATH = "resources/generate/";
 }