瀏覽代碼

浏览器查看、下载模板默认进入本地模板根路径,支持指定绝对路径;

sunyj 9 年之前
父節點
當前提交
154898d3db

+ 2 - 2
src/main/java/com/uas/report/controller/FileController.java

@@ -131,8 +131,8 @@ public class FileController {
 
 	@RequestMapping("/listFiles")
 	@ResponseBody
-	public List<Map<String, Object>> listFiles(String fileRelativePath) {
-		return fileService.listFiles(fileRelativePath);
+	public List<Map<String, Object>> listFiles(String filePath, Boolean isAbsolutePath) {
+		return fileService.listFiles(filePath, isAbsolutePath);
 	}
 
 }

+ 19 - 17
src/main/java/com/uas/report/service/FileService.java

@@ -96,32 +96,34 @@ public interface FileService {
 	/**
 	 * 递归删除文件(夹)
 	 * 
-	 * @param filePath
-	 *            文件(夹)路径
-	 * @return 文件(夹)路径
+	 * @param fileAbsolutePath
+	 *            文件(夹)绝对路径
+	 * @return 文件(夹)绝对路径
 	 */
-	public String delete(String filePath);
+	public String delete(String fileAbsolutePath);
 
 	/**
-	 * 列出本地资源根路径下指定路径的文件信息
+	 * 列出指定路径的文件信息
 	 * 
-	 * @param fileRelativePath
-	 *            指定的相对路径
-	 * @return 文件信息,包括name(String)、lastModified(String)、size(Long)、relativePath(
-	 *         String)、isDirectory(Boolean)
+	 * @param filePath
+	 *            指定的路径
+	 * @param isAbsolutePath
+	 *            文件路径是否为绝对路径
+	 * @return 文件信息,包括name(String)、lastModified(String)、size(Long)、filePath(
+	 *         String,可能是相对路径,也可能是绝对路径,具体取决于isAbsolutePath)、isDirectory(Boolean)
 	 */
-	public List<Map<String, Object>> listFiles(String fileRelativePath);
+	public List<Map<String, Object>> listFiles(String filePath, Boolean isAbsolutePath);
 
 	/**
 	 * 判断文件是否有效(文件存在并且未过有效期,并且比模板新)
 	 * 
-	 * @param filePath
+	 * @param fileAbsolutePath
 	 *            所指定的文件绝对路径
-	 * @param jrxmlFilePath
+	 * @param jrxmlFileAbsolutePath
 	 *            文件对应的模板路径
 	 * @return 是否有效
 	 */
-	public boolean isFileValid(String filePath, String jrxmlFilePath);
+	public boolean isFileValid(String fileAbsolutePath, String jrxmlFileAbsolutePath);
 
 	/**
 	 * 根据报表名、账套名等生成文件名(文件名:reportName + hashCode)
@@ -147,17 +149,17 @@ public interface FileService {
 	/**
 	 * 获取给定的pdf文件的页数
 	 * 
-	 * @param pdfFilePath
+	 * @param pdfFileAbsolutePath
 	 *            给定的pdf文件绝对路径
 	 * @return pdf文件的页数
 	 */
-	public int getPageSize(String pdfFilePath);
+	public int getPageSize(String pdfFileAbsolutePath);
 
 	/**
 	 * 利用给定的pdf文件在同级目录下生成多个分页的pdf文件(一页对应一个文件)
 	 * 
-	 * @param pdfFilePath
+	 * @param pdfFileAbsolutePath
 	 *            给定的pdf文件绝对路径
 	 */
-	public void createPagedPdfFiles(String pdfFilePath);
+	public void createPagedPdfFiles(String pdfFileAbsolutePath);
 }

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

@@ -251,29 +251,25 @@ public class FileServiceImpl implements FileService {
 	}
 
 	@Override
-	public String delete(String filePath) {
-		logger.info("request... " + filePath);
-		if (StringUtils.isEmpty(filePath)) {
+	public String delete(String fileAbsolutePath) {
+		logger.info("request... " + fileAbsolutePath);
+		if (StringUtils.isEmpty(fileAbsolutePath)) {
 			throw new ReportException("参数不能为空:filePath");
 		}
-		File file = new File(filePath);
+		File file = new File(fileAbsolutePath);
 		if (!file.exists()) {
-			throw new ReportException("文件不存在,不必删除:" + filePath);
+			throw new ReportException("文件不存在,不必删除:" + fileAbsolutePath);
 		}
 		FileUtils.deleteDir(file);
-		return filePath;
+		return fileAbsolutePath;
 	}
 
 	@Override
-	public List<Map<String, Object>> listFiles(String fileRelativePath) {
-		logger.info("request... " + fileRelativePath);
-		// 初始目录为本地资源根路径
-		String filePath = sysConf.getLocalBaseDir();
-		if (!StringUtils.isEmpty(fileRelativePath)) {
-			if (!fileRelativePath.startsWith("/")) {
-				filePath += "/";
-			}
-			filePath += fileRelativePath;
+	public List<Map<String, Object>> listFiles(String filePath, Boolean isAbsolutePath) {
+		logger.info("request... " + filePath);
+		if (isAbsolutePath == null || !isAbsolutePath) {
+			// 初始目录为本地资源根路径
+			filePath = sysConf.getLocalBaseDir() + "/" + filePath;
 		}
 		final File file = new File(filePath);
 		if (!file.exists()) {
@@ -283,7 +279,7 @@ public class FileServiceImpl implements FileService {
 		List<Map<String, Object>> result = new ArrayList<>();
 		// 如果是文件,直接获取文件信息
 		if (file.isFile()) {
-			result.add(getFileInformation(file));
+			result.add(getFileInformation(file, isAbsolutePath));
 		} else {
 			File[] files = file.listFiles(fileFilter);
 			// 文件夹放在前面展示
@@ -296,8 +292,8 @@ public class FileServiceImpl implements FileService {
 					fileList.add(f);
 				}
 			}
-			result.addAll(getFileInformations(directoryList));
-			result.addAll(getFileInformations(fileList));
+			result.addAll(getFileInformations(directoryList, isAbsolutePath));
+			result.addAll(getFileInformations(fileList, isAbsolutePath));
 		}
 		return result;
 	}
@@ -307,13 +303,15 @@ public class FileServiceImpl implements FileService {
 	 * 
 	 * @param files
 	 *            文件
-	 * @return 文件信息,包括name(String)、lastModified(String)、size(Long)、relativePath(
-	 *         String)、isDirectory(Boolean)
+	 * @param isAbsolutePath
+	 *            文件路径是否为绝对路径
+	 * @return 文件信息,包括name(String)、lastModified(String)、size(Long)、filePath(
+	 *         String,可能是相对路径,也可能是绝对路径,具体取决于isAbsolutePath)、isDirectory(Boolean)
 	 */
-	private List<Map<String, Object>> getFileInformations(List<File> files) {
+	private List<Map<String, Object>> getFileInformations(List<File> files, Boolean isAbsolutePath) {
 		List<Map<String, Object>> informationList = new ArrayList<>();
 		for (File file : files) {
-			informationList.add(getFileInformation(file));
+			informationList.add(getFileInformation(file, isAbsolutePath));
 		}
 		return informationList;
 	}
@@ -323,16 +321,22 @@ public class FileServiceImpl implements FileService {
 	 * 
 	 * @param file
 	 *            文件
-	 * @return 文件信息,包括name(String)、lastModified(String)、size(Long)、relativePath(
-	 *         String)、isDirectory(Boolean)
+	 * @param isAbsolutePath
+	 *            文件路径是否为绝对路径
+	 * @return 文件信息,包括name(String)、lastModified(String)、size(Long)、filePath(
+	 *         String,可能是相对路径,也可能是绝对路径,具体取决于isAbsolutePath)、isDirectory(Boolean)
 	 */
-	private Map<String, Object> getFileInformation(File file) {
+	private Map<String, Object> getFileInformation(File file, Boolean isAbsolutePath) {
 		if (file == null || !file.exists()) {
 			return null;
 		}
 		Map<String, Object> information = new HashMap<>();
 		information.put("name", file.getName());
-		information.put("relativePath", getRelativePath(file));
+		if (isAbsolutePath == null || !isAbsolutePath) {
+			information.put("filePath", getRelativePath(file));
+		} else {
+			information.put("filePath", file.getPath());
+		}
 		information.put("lastModified",
 				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified())));
 		if (file.isFile()) {
@@ -360,10 +364,10 @@ public class FileServiceImpl implements FileService {
 	}
 
 	@Override
-	public boolean isFileValid(String filePath, String jrxmlFilePath) {
-		if (!StringUtils.isEmpty(filePath) && !StringUtils.isEmpty(jrxmlFilePath)) {
-			File file = new File(filePath);
-			File jrxmlFile = new File(jrxmlFilePath);
+	public boolean isFileValid(String fileAbsolutePath, String jrxmlFileAbsolutePath) {
+		if (!StringUtils.isEmpty(fileAbsolutePath) && !StringUtils.isEmpty(jrxmlFileAbsolutePath)) {
+			File file = new File(fileAbsolutePath);
+			File jrxmlFile = new File(jrxmlFileAbsolutePath);
 			if (file.exists() && jrxmlFile.exists()) {
 				long interval = new Date().getTime() - file.lastModified();
 				// 剩余的有效期(最高为10分钟)
@@ -402,21 +406,22 @@ public class FileServiceImpl implements FileService {
 	}
 
 	@Override
-	public int getPageSize(String pdfFilePath) {
+	public int getPageSize(String pdfFileAbsolutePath) {
 		try {
-			return new PdfReader(pdfFilePath).getNumberOfPages();
+			return new PdfReader(pdfFileAbsolutePath).getNumberOfPages();
 		} catch (IOException e) {
 			throw new ReportException(e).setDetailedMessage(e);
 		}
 	}
 
 	@Override
-	public void createPagedPdfFiles(String pdfFilePath) {
+	public void createPagedPdfFiles(String pdfFileAbsolutePath) {
 		try {
-			PdfReader pdfReader = new PdfReader(pdfFilePath);
+			PdfReader pdfReader = new PdfReader(pdfFileAbsolutePath);
 			for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
 				Document document = new Document();
-				FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath.replace(".pdf", "_" + i + ".pdf"));
+				FileOutputStream fileOutputStream = new FileOutputStream(
+						pdfFileAbsolutePath.replace(".pdf", "_" + i + ".pdf"));
 				PdfCopy pdfCopy = new PdfCopy(document, fileOutputStream);
 				document.open();
 				// 原pdf文件的每一页生成新的pdf

+ 2 - 2
src/main/webapp/WEB-INF/views/console.html

@@ -41,7 +41,7 @@
 				<strong><li class="title1">查看</li></strong>
 				<ol>
 					<li><a target="_blank">files</a></li>
-					<li><a target="_blank">files?relativePath=UAS/jrxml</a></li>
+					<li><a target="_blank">files?filePath=C:/sunyj/reports</a></li>
 				</ol>
 
 				<strong><li class="title1">上传</li></strong>
@@ -101,7 +101,7 @@
 				</ol>
 				<strong><li class="title2">B2B</li></strong>
 				<ol>
-					<li><a target="_blank">print?userName=B2B/1111&profile=test&reportName=order&whereCondition=where
+					<li><a target="_blank">print?userName=B2B/10005740&profile=test&reportName=order&whereCondition=where
 							%20rownum<30</a></li>
 					<li><a target="_blank">fileUpload?userName=B2B/1111</a></li>
 				</ol>

+ 2 - 2
src/main/webapp/WEB-INF/views/files.html

@@ -13,10 +13,10 @@
 			<div id="currentPathContainer">
 				<span>当前路径:<span id="currentPath"></span></span>
 			</div>
-			<div id="searchContainer">
+			<div id="searchContainer" hidden="true">
 				<span>搜索: <input class="search" /></span>
 			</div>
-			<br/><br/>
+			<br /><br />
 			<div id="listHeaderContainer">
 				<div class="listHeader columnNameHeader">
 					<span>名称</span>

+ 37 - 16
src/main/webapp/resources/js/files/app.js

@@ -1,8 +1,9 @@
+//获取链接中的参数,如果存在,则表示进入指定的绝对路径
+var filePathParameter = getParameter("filePath");
 var currentPath = new Object();
-currentPath.value = getParameter("relativePath") || "/";
-if (currentPath.value.charAt(0) != "/") {
-	currentPath.value = "/" + currentPath.value;
-}
+// 如果不存在filePath参数,表示进入相对路径(相对于所配置的本地资源根路径)
+currentPath.value = filePathParameter || "/";
+
 listFiles(currentPath.value);
 
 $("#parentPath").click(function() {
@@ -20,15 +21,20 @@ $("#parentPath").click(function() {
  *            文件路径
  */
 function listFiles(path) {
+	console.log(JSON.stringify(currentPath));
 	$("#currentPath").text(currentPath.value);
-	if(!currentPath.parent){
-		$("#parentPathContainer").attr("hidden","true");
-	}else{
+	if (!currentPath.parent) {
+		$("#parentPathContainer").attr("hidden", "true");
+	} else {
 		$("#parentPathContainer").removeAttr("hidden");
 	}
 	var listFilesUrl = "file/listFiles";
 	if (path) {
-		listFilesUrl += "?fileRelativePath=" + path;
+		listFilesUrl += "?filePath=" + path;
+	}
+	// 如果链接中指定路径,则表示请求的绝对路径
+	if (filePathParameter) {
+		listFilesUrl += "&isAbsolutePath=true";
 	}
 	$.ajax({
 		type : "get",
@@ -37,7 +43,7 @@ function listFiles(path) {
 			$(".listItem").remove();
 			for (var i = 0; i < data.length; i++) {
 				addList(data[i].name, data[i].size, data[i].lastModified,
-						data[i].relativePath, data[i].isDirectory);
+						data[i].filePath, data[i].isDirectory);
 			}
 		},
 		error : function(XMLHttpRequest) {
@@ -66,12 +72,12 @@ function listFiles(path) {
  *            文件大小
  * @param lastModified
  *            文件最近修改时间
- * @param relativePath
+ * @param filePath
  *            文件相对路径
  * @param isDirectory
  *            是否为文件夹
  */
-function addList(name, size, lastModified, relativePath, isDirectory) {
+function addList(name, size, lastModified, filePath, isDirectory) {
 	if (size || size == 0) {
 		size += " B";
 	} else {
@@ -105,15 +111,30 @@ function addList(name, size, lastModified, relativePath, isDirectory) {
 			// 记录父目录
 			var pathCopy = currentPath;
 			currentPath = new Object();
-			currentPath.value = relativePath;
+			currentPath.value = filePath;
 			currentPath.parent = pathCopy;
 			listFiles(currentPath.value);
 		} else {
-			window.open("file/download?filePath=" + relativePath);
+			download(filePath);
 		}
 	});
-	
-	downloadDiv.click(function(){
-		window.open("file/download?filePath=" + relativePath);
+
+	downloadDiv.click(function() {
+		download(filePath);
 	});
 }
+
+/**
+ * 下载文件(夹)
+ * 
+ * @param path
+ *            文件(夹)路径
+ */
+function download(path) {
+	var downloadUrl = "file/download?filePath=" + path;
+	// 如果链接中指定路径,则表示请求的绝对路径
+	if (filePathParameter) {
+		downloadUrl += "&isAbsolutePath=true";
+	}
+	window.open(downloadUrl);
+}