Parcourir la source

generated pdfs and excels are not placed in resources/generate/

sunyj il y a 8 ans
Parent
commit
2c17527b8c

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

@@ -99,12 +99,12 @@ public class PdfController {
 			}
 			byte[] data = (byte[]) result.remove("data");
 			// 相对路径
-			String pdfPath = ReportConstants.GENERATED_FILES_PATH + r + "/"
-					+ fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier()) + "."
-					+ ExportType.PDF.getQualifier();
-			File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
+			String pdfPath = r + "/"
+					+ fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier())
+                    + "." + ExportType.PDF.getQualifier();
+			File file = new File(ReportUtils.getDocumentDir(), pdfPath);
 			FileUtils.write(file.getPath(), data);
-			result.put("path", pdfPath);
+			result.put("path", "pdf/preview?p=" + pdfPath);
 			result.put("overload", false);
 		}
 		return result;
@@ -210,10 +210,10 @@ public class PdfController {
 		ReportUtils.checkParameters(u, r);
 
 		// 相对路径
-		String pdfPath = ReportConstants.GENERATED_FILES_PATH + r + "/"
-				+ fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier()) + "."
-				+ ExportType.PDF.getQualifier();
-		File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
+		String pdfPath = r + "/"
+				+ fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier())
+                + "." + ExportType.PDF.getQualifier();
+		File file = new File(ReportUtils.getDocumentDir(), pdfPath);
 		String masterOfJrxml = printService.getMasterOfJrxml(u, r);
 		String jrxmlFilePath = fileService.getJrxmlFilePath(masterOfJrxml, r);
 		if (!fileService.isFileValid(file.getPath(), jrxmlFilePath)) {
@@ -236,9 +236,9 @@ public class PdfController {
 	 */
 	@RequestMapping(value = "/preview")
 	@ResponseBody
-	public void preview(@RequestParam(required = true) String p, HttpServletRequest request, HttpServletResponse response)
+	public void preview(@RequestParam String p, HttpServletRequest request, HttpServletResponse response)
 			throws JRException, IOException, DocumentException, SQLException, IllegalStateException {
-		File file = new File(ReportConstants.GENERATED_FILES_DIR + p);
+		File file = new File(ReportUtils.getDocumentDir(), p);
 		if(!file.getName().toLowerCase().endsWith(".pdf")){
 			throw new IOException("并非 pdf 文件:" + p);
 		}

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

@@ -147,10 +147,10 @@ public class PrintController {
 		ExportType exportType = StringUtils.isEmpty(exportFileType) ? ExportType.PDF
 				: ExportType.checkType(exportFileType);
 
-		String filePath = ReportConstants.GENERATED_FILES_PATH + reportName + "/"
-				+ fileService.generateFileName(userName, profile, whereCondition, otherParameters, exportFileType) + "."
-				+ exportType.getQualifier();
-		File file = new File(ReportConstants.GENERATED_FILES_DIR + filePath);
+		String filePath = reportName + "/"
+				+ fileService.generateFileName(userName, profile, whereCondition, otherParameters, exportFileType)
+                + "." + exportType.getQualifier();
+		File file = new File(ReportUtils.getDocumentDir(), filePath);
 		// 指定flush为true(强制刷新pdf、xls)
 		// 文件无效(不存在或过期),创建
 		if ((flush != null && flush)
@@ -203,12 +203,12 @@ public class PrintController {
 		ReportUtils.checkParameters(userName, reportName);
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 
-		// 相对路径,返回给前端
-		String pdfPath = ReportConstants.GENERATED_FILES_PATH + reportName + "/" + fileService
-				.generateFileName(userName, profile, whereCondition, otherParameters, ExportType.PDF.getQualifier())
-				+ "." + ExportType.PDF.getQualifier();
-		File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
-		// 指定flush为true(强制刷新pdf)
+        // 相对路径,返回给前端
+        String pdfPath = reportName + "/"
+                + fileService.generateFileName(userName, profile, whereCondition, otherParameters, ExportType.PDF.getQualifier())
+                + "." + ExportType.PDF.getQualifier();
+        File file = new File(ReportUtils.getDocumentDir(), pdfPath);
+        // 指定flush为true(强制刷新pdf)
 		// 文件无效(不存在或过期),重新创建pdf文件
 		if ((flush != null && flush)
 				|| !fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(masterOfJrxml, reportName))) {

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

@@ -592,7 +592,7 @@ public class FileServiceImpl implements FileService {
 		Executable command = new Executable() {
 			@Override
 			public String execute() {
-				FileUtils.delete(new File(ReportConstants.GENERATED_FILES_ABSOLUTE_PATH), new FileFilter() {
+				FileUtils.delete(new File(ReportUtils.getTmpDir()), new FileFilter() {
 					@Override
 					public boolean accept(File file) {
 						// 只删除已过期的文件

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

@@ -118,7 +118,7 @@ public class PrintServiceImpl implements PrintService {
 		// 向报表模板传递参数:报表路径、where条件、其他参数
 		Map<String, Object> parameters = new HashMap<>();
         if (useFileVirtualizer) {
-            File virtualizerDir = new File(fileService.getMasterPath("tmp") + "/virtualizer");
+            File virtualizerDir = new File(ReportUtils.getVirtualizerDir());
             if (!virtualizerDir.exists()) {
                 virtualizerDir.mkdirs();
             }

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

@@ -18,21 +18,6 @@ public class ReportConstants {
 	 */
 	public static final String PARAMETER_REPORT_DIR = "REPORT_DIR";
 
-	/**
-	 * 生成的pdf、excel等文件的相对路径
-	 */
-	public static final String GENERATED_FILES_PATH = "resources/generate/";
-
-	/**
-	 * 生成的pdf、excel等文件的所在的文件夹
-	 */
-	public static final String GENERATED_FILES_DIR = PathUtils.getAppPath();
-
-	/**
-	 * 生成的pdf、excel等文件的绝对路径
-	 */
-	public static final String GENERATED_FILES_ABSOLUTE_PATH = GENERATED_FILES_DIR + GENERATED_FILES_PATH;
-
 	/**
 	 * 微软雅黑字体名称
 	 */

+ 21 - 0
src/main/java/com/uas/report/util/ReportUtils.java

@@ -45,4 +45,25 @@ public class ReportUtils {
 			taskService.start();
 		}
 	}
+
+	/**
+	 * @return 临时路径
+	 */
+	public static String getTmpDir() {
+		return ContextUtils.getBean(FileService.class).getMasterPath("tmp") + "/generate";
+	}
+
+	/**
+	 * @return 文件虚拟路径
+	 */
+	public static String getVirtualizerDir() {
+		return getTmpDir() + "/virtualizer";
+	}
+
+	/**
+	 * @return pdf、excel 等文档路径
+	 */
+	public static String getDocumentDir() {
+		return getTmpDir() + "/documents";
+	}
 }