Browse Source

支持单文件、zip下载

sunyj 9 years ago
parent
commit
ee11851522

+ 71 - 0
src/main/java/com/uas/report/controller/DownloadController.java

@@ -0,0 +1,71 @@
+package com.uas.report.controller;
+
+import javax.servlet.http.HttpServletResponse;
+
+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 com.uas.report.core.exception.ReportException;
+import com.uas.report.service.DownloadService;
+
+/**
+ * 下载请求
+ * 
+ * @author sunyj
+ * @since 2016年11月2日 下午2:05:04
+ */
+@Controller
+@RequestMapping("/download")
+public class DownloadController {
+
+	@Autowired
+	private DownloadService downloadService;
+
+	/**
+	 * 下载文件
+	 * 
+	 * @param filePath
+	 *            文件路径
+	 * @param response
+	 */
+	@RequestMapping("")
+	public void download(String filePath, HttpServletResponse response) {
+		downloadService.download(filePath, response);
+	}
+
+	/**
+	 * 下载账套下的所有资源
+	 * 
+	 * @param userName
+	 *            账套名
+	 * @param response
+	 */
+	@RequestMapping("/zip")
+	public void downloadZip(String userName, HttpServletResponse response) {
+		if (StringUtils.isEmpty(userName)) {
+			throw new ReportException("未传入当前账套用户名!");
+		}
+		String zipFilePath = downloadService.getZip(userName);
+		if (zipFilePath.isEmpty()) {
+			throw new ReportException("压缩失败");
+		}
+		downloadService.download(zipFilePath, response);
+	}
+
+	/**
+	 * 下载模板
+	 * 
+	 * @param userName
+	 *            账套名称
+	 * @param reportName
+	 *            模板名称
+	 * @param response
+	 */
+	@RequestMapping("/jrxml")
+	public void downloadJrxml(String userName, String reportName, HttpServletResponse response) {
+		downloadService.download(downloadService.getJrxml(userName, reportName), response);
+	}
+
+}

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

@@ -23,6 +23,7 @@ import com.uas.report.core.exception.ReportException;
 import com.uas.report.service.PrintService;
 import com.uas.report.util.PathUtils;
 import com.uas.report.util.ReportConstants;
+import com.uas.report.util.ReportUtils;
 
 /**
  * 报表打印
@@ -109,7 +110,7 @@ public class PrintController {
 	@ResponseBody
 	public void export(String userName, String profile, String reportName, String whereCondition,
 			String otherParameters, String exportFileType, HttpServletResponse response) {
-		checkParameters(userName, reportName);
+		ReportUtils.checkParameters(userName, reportName);
 
 		logger.info("开始导出报表:" + reportName);
 		if (StringUtils.isEmpty(exportFileType)) {
@@ -193,7 +194,7 @@ public class PrintController {
 	@ResponseBody
 	public Map<String, Object> loadPdfData(String userName, String profile, String reportName, String whereCondition,
 			String otherParameters, Integer pageIndex, HttpServletResponse response) {
-		checkParameters(userName, reportName);
+		ReportUtils.checkParameters(userName, reportName);
 		logger.info("开始预览报表:" + reportName);
 		Map<String, Object> result = null;
 
@@ -278,20 +279,4 @@ public class PrintController {
 		return result;
 	}
 
-	/**
-	 * 检查userName、reportName参数是否有效,无效则抛出异常
-	 * 
-	 * @param userName
-	 *            账套名
-	 * @param reportName
-	 *            报表名
-	 */
-	private void checkParameters(String userName, String reportName) {
-		if (StringUtils.isEmpty(userName)) {
-			throw new ReportException("未传入当前账套用户名!");
-		}
-		if (StringUtils.isEmpty(reportName)) {
-			throw new ReportException("未传入报表名称!");
-		}
-	}
 }

+ 41 - 0
src/main/java/com/uas/report/service/DownloadService.java

@@ -0,0 +1,41 @@
+package com.uas.report.service;
+
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * 下载操作
+ * 
+ * @author sunyj
+ * @since 2016年11月2日 下午2:36:46
+ */
+public interface DownloadService {
+
+	/**
+	 * 获取账套下的某个模板
+	 * 
+	 * @param userName
+	 *            账套名称
+	 * @param reportName
+	 *            模板名称
+	 * @return 模板路径
+	 */
+	public String getJrxml(String userName, String reportName);
+
+	/**
+	 * 获取账套下的所有资源
+	 * 
+	 * @param userName
+	 *            账套名称
+	 * @return 账套下所有资源的压缩包路径
+	 */
+	public String getZip(String userName);
+
+	/**
+	 * 下载文件
+	 * 
+	 * @param filePath
+	 *            文件路径
+	 * @param response
+	 */
+	public void download(String filePath, HttpServletResponse response);
+}

+ 72 - 0
src/main/java/com/uas/report/service/impl/DownloadServiceImpl.java

@@ -0,0 +1,72 @@
+package com.uas.report.service.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.alibaba.druid.util.StringUtils;
+import com.uas.report.core.exception.ReportException;
+import com.uas.report.service.DownloadService;
+import com.uas.report.support.JasperserverRestAPIConf;
+import com.uas.report.util.ReportUtils;
+import com.uas.report.util.ZipUtils;
+
+@Service
+public class DownloadServiceImpl implements DownloadService {
+
+	@Autowired
+	private JasperserverRestAPIConf jsRestAPIConf;
+
+	@Override
+	public String getJrxml(String userName, String reportName) {
+		ReportUtils.checkParameters(userName, reportName);
+		return new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append(File.separator).append(userName)
+				.append(File.separator).append(jsRestAPIConf.getLocalJrxmlDir()).append(File.separator)
+				.append(reportName).append(".jrxml").toString();
+	}
+
+	@Override
+	public String getZip(String userName) {
+		// 账套路径
+		String folderPath = jsRestAPIConf.getLocalBaseDir() + File.separator + userName;
+		// 压缩后的压缩包路径,与账套在同一级
+		String zipFilePath = folderPath + ".zip";
+		ZipUtils.zipFolder(folderPath, zipFilePath);
+		return zipFilePath;
+	}
+
+	@Override
+	public void download(String filePath, HttpServletResponse response) {
+		if (StringUtils.isEmpty(filePath) || response == null) {
+			throw new ReportException("参数不能为空:filePath,response");
+		}
+		File file = new File(filePath);
+		if (!file.exists()) {
+			throw new ReportException("文件不存在:" + filePath);
+		}
+		if (!file.isFile()) {
+			throw new ReportException("并非文件:" + filePath);
+		}
+		try {
+			InputStream inputStream = new FileInputStream(file);
+			byte[] data = new byte[inputStream.available()];
+			inputStream.read(data);
+			response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
+			OutputStream outputStream = response.getOutputStream();
+			outputStream.write(data);
+			outputStream.flush();
+			inputStream.close();
+			outputStream.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+}

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

@@ -304,8 +304,8 @@ public class PrintServiceImpl implements PrintService {
 
 		// 报表路径为报表根路径REPORT_DIR + 当前账套用户名userName
 		String reportDir = new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/").append(userName).toString();
-		String jrxmlFilePath = new StringBuilder(reportDir).append("/").append("jrxml").append("/").append(reportName)
-				.append(".jrxml").toString();
+		String jrxmlFilePath = new StringBuilder(reportDir).append("/").append(jsRestAPIConf.getLocalJrxmlDir())
+				.append("/").append(reportName).append(".jrxml").toString();
 		File jrxmlFile = new File(jrxmlFilePath);
 		// 报表模板不存在
 		if (!jrxmlFile.exists()) {

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

@@ -0,0 +1,30 @@
+package com.uas.report.util;
+
+import org.springframework.util.StringUtils;
+
+import com.uas.report.core.exception.ReportException;
+
+/**
+ * 工具类
+ * 
+ * @author sunyj
+ * @since 2016年11月2日 下午2:51:24
+ */
+public class ReportUtils {
+	/**
+	 * 检查userName、reportName参数是否有效,无效则抛出异常
+	 * 
+	 * @param userName
+	 *            账套名
+	 * @param reportName
+	 *            报表名
+	 */
+	public static void checkParameters(String userName, String reportName) {
+		if (StringUtils.isEmpty(userName)) {
+			throw new ReportException("未传入当前账套用户名!");
+		}
+		if (StringUtils.isEmpty(reportName)) {
+			throw new ReportException("未传入报表名称!");
+		}
+	}
+}

+ 40 - 31
src/main/webapp/WEB-INF/views/index.html

@@ -7,51 +7,60 @@
 </head>
 <body>
 	<div id="mainContainer">
-		<ol>
-			<strong><li class="title1">预览</li></strong>
+		<div id="leftContainer">
+			<h2>1. 预览、打印导出</h2>
 			<ol>
-				<u><li class="href">print?userName=UAS&reportName=Purchase&whereCondition=where
-						%20rownum<10&otherParameters={'id':1}&printType=PREVIEW</li></u>
-				<u><li class="href">preview?userName=UAS&reportName=Purchase&whereCondition=where
-						%20rownum<10&printType=PREVIEW</li></u>
-				<u><li class="href">preview2?userName=UAS&reportName=Purchase&whereCondition=where
-						%20rownum<10&printType=PREVIEW</li></u>
-			</ol>
+				<strong><li class="title1">预览</li></strong>
+				<ol>
+					<u><li class="href">print?userName=UAS&reportName=Purchase&whereCondition=where
+							%20rownum<10&otherParameters={'id':1}&printType=PREVIEW</li></u>
+					<u><li class="href">preview?userName=UAS&reportName=Purchase&whereCondition=where
+							%20rownum<11&printType=PREVIEW</li></u>
+					<u><li class="href">preview2?userName=UAS&reportName=Purchase&whereCondition=where
+							%20rownum<12&printType=PREVIEW</li></u>
+				</ol>
 
-			<strong><li class="title1">打印</li></strong>
-			<ol>
-				<u><li class="href">print?userName=UAS&reportName=Purchase&whereCondition=where
-						%20rownum<10&printType=PRINT</li></u>
-			</ol>
+				<strong><li class="title1">打印</li></strong>
+				<ol>
+					<u><li class="href">print?userName=UAS&reportName=Purchase&whereCondition=where
+							%20rownum<13&printType=PRINT</li></u>
+				</ol>
 
 
-			<strong><li class="title1">导出</li></strong>
-			<ol>
-				<u><li class="href">print?userName=UAS&reportName=Purchase&whereCondition=where
-						%20rownum<10&printType=PDF</li></u>
-				<u><li class="href">print?userName=UAS&reportName=Purchase&whereCondition=where
-						%20rownum<10&printType=EXCEL</li></u>
+				<strong><li class="title1">导出</li></strong>
+				<ol>
+					<u><li class="href">print?userName=UAS&reportName=Purchase&whereCondition=where
+							%20rownum<14&printType=PDF</li></u>
+					<u><li class="href">print?userName=UAS&reportName=Purchase&whereCondition=where
+							%20rownum<15&printType=EXCEL</li></u>
+				</ol>
 			</ol>
-
-			<strong><li class="title1">上传</li></strong>
+		</div>
+		<div id="rightContainer">
+			<h2>2. 上传、下载</h2>
 			<ol>
-				<u><li class="href">fileUpload?userName=UAS</li></u>
-				<u><li class="href">fileUpload?userName=tmp</li></u>
-			</ol>
+				<strong><li class="title1">上传</li></strong>
+				<ol>
+					<u><li class="href">fileUpload?userName=UAS</li></u>
+					<u><li class="href">fileUpload?userName=tmp</li></u>
+				</ol>
 
-			<strong><li class="title1">下载</li></strong>
-			<ol>
-				<u><li class="href">XXX</li></u>
+				<strong><li class="title1">下载</li></strong>
+				<ol>
+					<u><li class="href">download?filePath=C:/sunyj/过程文档/sql/aq.sql</li></u>
+					<u><li class="href">download/zip?userName=UAS</li></u>
+					<u><li class="href">download/jrxml?userName=UAS&reportName=Purchase</li></u>
+				</ol>
 			</ol>
 
-			<strong><li class="title1">接口</li></strong>
+			<h2>3. 接口</h2>
 			<ol>
 				<strong><li class="title2">打印接口</li></strong>
 				<ol>
 					<u><li class="href">print/loadPdfData?userName=UAS&reportName=Purchase&whereCondition=where
 							%20rownum<30&pageIndex=2</li></u>
 					<u><li class="href">print/export?userName=UAS&reportName=Purchase&whereCondition=where
-							%20rownum<10&exportFileType=xls_with_only_data</li></u>
+							%20rownum<16&exportFileType=xls_with_only_data</li></u>
 				</ol>
 				<strong><li class="title2">资源接口</li></strong>
 				<ol>
@@ -65,7 +74,7 @@
 							%20rownum<30</li></u>
 				</ol>
 			</ol>
-		</ol>
+		</div>
 	</div>
 </body>
 

+ 14 - 0
src/main/webapp/resources/css/fileUpload.css

@@ -0,0 +1,14 @@
+* {
+	font-family: microsoft yahei ui;
+}
+
+body {
+	margin: 20px;
+}
+
+#upload {
+	margin-left: 10px;
+	padding: 1px 15px 1px 15px;
+	border-radius: 10%;
+	background-color: #eee;
+}

+ 26 - 8
src/main/webapp/resources/css/index.css

@@ -1,23 +1,41 @@
 body {
 	font-family: courier;
-	margin: 30px;
 }
 
-li.title1 {
-	font-size: 20px;
-	margin-top: 20px;
+h2 {
+	margin-top: 10px;
 	margin-bottom: 10px;
-	color: #404040;
 }
 
-li.title2 {
-	font-size: 15px;
+#leftContainer, #rightContainer {
+	word-wrap: break-world;
+	word-break: break-all;
+}
+
+#leftContainer {
+	float: left;
+	width: 38%;
+	margin-left: 2%;
+	margin-right: 2%;
+}
+
+#rightContainer {
+	float: right;
+	width: 54%;
+	margin-left: 2%;
+	margin-right: 2%;
+}
+
+li.title {
+	margin-top: 10px;
+	margin-bottom: 10px;
 	color: #404040;
 }
 
 li.href {
-	border: 0;
+	font-size: 14.5px;
 	cursor: pointer;
 	outline: none;
 	margin-bottom: 10px;
+	border: 0;
 }