Sfoglia il codice sorgente

增加删除模板文件的接口

sunyj 9 anni fa
parent
commit
1498363963

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

@@ -120,13 +120,8 @@ public class FileController {
 
 	@RequestMapping("/delete")
 	@ResponseBody
-	public String delete(String userName, String filePath) {
-		// 账套为空,filePath为绝对路径,否则为账套下的相对路径
-		if (StringUtils.isEmpty(userName)) {
-			return "Deleted... " + fileService.delete(filePath);
-		} else {
-			return "Deleted... " + fileService.delete(userName, filePath);
-		}
+	public String delete(String filePath, Boolean isAbsolutePath) {
+		return "Deleted... " + fileService.delete(filePath, isAbsolutePath);
 	}
 
 	@RequestMapping("/listFiles")

+ 5 - 14
src/main/java/com/uas/report/service/FileService.java

@@ -82,25 +82,16 @@ public interface FileService {
 	 */
 	public void download(String filePath, Boolean isAbsolutePath, HttpServletResponse response);
 
-	/**
-	 * 账套下的某个文件(夹)
-	 * 
-	 * @param userName
-	 *            账套名称
-	 * @param fileRelativePath
-	 *            文件(夹)在账套下的相对路径
-	 * @return 文件(夹)路径
-	 */
-	public String delete(String userName, String fileRelativePath);
-
 	/**
 	 * 递归删除文件(夹)
 	 * 
-	 * @param fileAbsolutePath
-	 *            文件(夹)绝对路径
+	 * @param filePath
+	 *            文件(夹)路径
+	 * @param isAbsolutePath
+	 *            文件(夹)路径是否为绝对路径
 	 * @return 文件(夹)绝对路径
 	 */
-	public String delete(String fileAbsolutePath);
+	public String delete(String filePath, Boolean isAbsolutePath);
 
 	/**
 	 * 列出指定路径的文件信息

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

@@ -210,9 +210,7 @@ public class FileServiceImpl implements FileService {
 			throw new ReportException("参数不能为空:filePath,response");
 		}
 		logger.info("request... " + filePath);
-		if (isAbsolutePath == null || !isAbsolutePath) {
-			filePath = sysConf.getLocalBaseDir() + "/" + filePath;
-		}
+		filePath = getAbsolutePath(filePath, isAbsolutePath);
 		File file = new File(filePath);
 		if (!file.exists()) {
 			throw new ReportException("文件不存在:" + filePath);
@@ -243,34 +241,29 @@ public class FileServiceImpl implements FileService {
 	}
 
 	@Override
-	public String delete(String userName, String fileRelativePath) {
-		if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(fileRelativePath)) {
-			throw new ReportException("参数不能为空:userName,fileRelativePath");
+	public String delete(String filePath, Boolean isAbsolutePath) {
+		logger.info("request... " + filePath);
+		// 路径不能为空,不能只包含'/'(根路径),不能含有'..'(不允许通过该方式删除上一级)
+		if (StringUtils.isEmpty(filePath) || StringUtils.isEmpty(filePath.replaceAll("/", ""))
+				|| filePath.contains("..")) {
+			throw new ReportException("路径不合法:" + filePath);
 		}
-		return delete(getMasterPath(userName) + "/" + fileRelativePath);
-	}
-
-	@Override
-	public String delete(String fileAbsolutePath) {
-		logger.info("request... " + fileAbsolutePath);
-		if (StringUtils.isEmpty(fileAbsolutePath)) {
+		filePath = getAbsolutePath(filePath, isAbsolutePath);
+		if (StringUtils.isEmpty(filePath)) {
 			throw new ReportException("参数不能为空:filePath");
 		}
-		File file = new File(fileAbsolutePath);
+		File file = new File(filePath);
 		if (!file.exists()) {
-			throw new ReportException("文件不存在,不必删除:" + fileAbsolutePath);
+			throw new ReportException("文件不存在,不必删除:" + filePath);
 		}
 		FileUtils.deleteDir(file);
-		return fileAbsolutePath;
+		return filePath;
 	}
 
 	@Override
 	public List<Map<String, Object>> listFiles(String filePath, Boolean isAbsolutePath) {
 		logger.info("request... " + filePath);
-		if (isAbsolutePath == null || !isAbsolutePath) {
-			// 初始目录为本地资源根路径
-			filePath = sysConf.getLocalBaseDir() + "/" + filePath;
-		}
+		filePath = getAbsolutePath(filePath, isAbsolutePath);
 		final File file = new File(filePath);
 		if (!file.exists()) {
 			throw new ReportException("文件不存在:" + filePath);
@@ -437,4 +430,24 @@ public class FileServiceImpl implements FileService {
 		}
 	}
 
+	/**
+	 * 根据指定路径获取绝对路径,如果isAbsolutePath为真,直接返回指定路径,否则拼接上模板根路径
+	 * 
+	 * @param filePath
+	 *            指定路径
+	 * @param isAbsolutePath
+	 *            是否为绝对路径
+	 * @return 绝对路径
+	 */
+	private String getAbsolutePath(String filePath, Boolean isAbsolutePath) {
+		if (StringUtils.isEmpty(filePath)) {
+			throw new ReportException("路径不合法:" + filePath);
+		}
+		// 不是绝对路径的话,则相对于模板根路径
+		if (isAbsolutePath == null || !isAbsolutePath) {
+			filePath = sysConf.getLocalBaseDir() + "/" + filePath;
+		}
+		return filePath;
+	}
+
 }

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

@@ -40,6 +40,7 @@
 				<ol>
 					<li><a target="_blank">files</a></li>
 					<li><a target="_blank">files?filePath=C:/sunyj/reports</a></li>
+					<li><a target="_blank">files?filePath=C:\Users\sunyj-pc\Downloads&showDelete=true</a></li>
 				</ol>
 
 				<strong><li class="title1">上传</li></strong>
@@ -56,8 +57,8 @@
 				</ol>
 				<strong><li class="title1">删除</li></strong>
 				<ol>
-					<li><a target="_blank">file/delete?filePath=C:/test</a></li>
-					<li><a target="_blank">file/delete?userName=UAS&filePath=Picture/1.jpg</a></li>
+					<li><a target="_blank">file/delete?filePath=UAS/Picture/1.jpg</a></li>
+					<li><a target="_blank">file/delete?filePath=C:/test&isAbsolutePath=true</a></li>
 				</ol>
 			</ol>
 

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

@@ -16,7 +16,7 @@
 			<div id="searchContainer" hidden="true">
 				<span>搜索: <input class="search" /></span>
 			</div>
-			<br /><br />
+			<br /> <br />
 			<div id="listHeaderContainer">
 				<div class="listHeader columnNameHeader">
 					<span>名称</span>
@@ -27,8 +27,8 @@
 				<div class="listHeader columnLastModified">
 					<span>修改日期</span>
 				</div>
-				<div class="listHeader columnDownload">
-					<span>下载</span>
+				<div class="listHeader columnOperation">
+					<span>操作</span>
 				</div>
 			</div>
 		</div>
@@ -47,6 +47,6 @@
 </body>
 
 <script src="static/lib/jquery/jquery.min.js"></script>
-<script src="static/js/preview/utils.js"></script>
+<script src="static/js/util/utils.js"></script>
 <script src="static/js/files/app.js"></script>
 </html>

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

@@ -79,6 +79,5 @@
 <script src="static/lib/jquery/ba-tiny-pubsub.min.js"></script>
 <script src="static/lib/spin/spin.min.js"></script>
 <script src="static/js/util/utils.js"></script>
-<script src="static/js/preview/utils.js"></script>
 <script src="static/js/preview/app.js"></script>
 </html>

+ 1 - 1
src/main/webapp/resources/css/files.css

@@ -28,7 +28,7 @@ body {
 	text-align: left;
 }
 
-.columnSize, .columnDownload {
+.columnSize, .columnOperation {
 	width: 15%;
 	float: left;
 	text-align: center;

+ 76 - 15
src/main/webapp/resources/js/files/app.js

@@ -1,5 +1,6 @@
 //获取链接中的参数,如果存在,则表示进入指定的绝对路径
 var filePathParameter = getParameter("filePath");
+var showDelete = getParameter("showDelete");
 var currentPath = new Object();
 // 如果不存在filePath参数,表示进入相对路径(相对于所配置的本地资源根路径)
 currentPath.value = filePathParameter || "/";
@@ -41,24 +42,16 @@ function listFiles(path) {
 		url : listFilesUrl,
 		success : function(data) {
 			$(".listItem").remove();
+			if (!data.length) {
+				showErrorMessage("文件数量为0");
+			}
 			for (var i = 0; i < data.length; i++) {
 				addList(data[i].name, data[i].size, data[i].lastModified,
 						data[i].filePath, data[i].isDirectory);
 			}
 		},
 		error : function(XMLHttpRequest) {
-			$("#errorMessageContainer").removeAttr("hidden");
-			// 处理后台传输的自定义的换行标志
-			var result = JSON.parse(XMLHttpRequest.responseText);
-			var message = result.message;
-			$("#message").html(message);
-			if (result.detailedMessage) {
-				$("#detailedMessageButton").removeAttr("hidden");
-			}
-			$("#detailedMessageButton").click(function() {
-				$("#detailedMessage").html(result.detailedMessage);
-				$("#detailedMessage").removeAttr("hidden");
-			});
+			showErrorMessage(XMLHttpRequest);
 		}
 	});
 }
@@ -107,11 +100,16 @@ function addList(name, size, lastModified, filePath, isDirectory) {
 	var sizeDiv = $("<div class='columnSize'><span>" + size + "</span></div>");
 	var lastModifiedDiv = $("<div class='columnLastModified'><span>"
 			+ lastModified + "</span></div>");
-	var downloadDiv = $("<div class='columnDownload'><button id='downloadButton'><i class='fa fa-cloud-download fa-lg' aria-hidden='true'></i></button></div>");
+
+	var downloadButton = $("<button id='downloadButton' title='下载'><i class='fa fa-cloud-download fa-lg' aria-hidden='true'></i></button>");
+	var deleteButton = $("<button id='deleteButton' title='删除'><i class='fa fa-trash fa-lg' aria-hidden='true'></i>");
+	var operationDiv = $("<div class='columnOperation'></div>");
+	operationDiv.append(downloadButton);
+
 	listItemDiv.append(nameDiv);
 	listItemDiv.append(sizeDiv);
 	listItemDiv.append(lastModifiedDiv);
-	listItemDiv.append(downloadDiv);
+	listItemDiv.append(operationDiv);
 	$('#listContentContainer').append(listItemDiv);
 
 	nameDiv.click(function() {
@@ -127,9 +125,16 @@ function addList(name, size, lastModified, filePath, isDirectory) {
 		}
 	});
 
-	downloadDiv.click(function() {
+	downloadButton.click(function() {
 		download(filePath);
 	});
+
+	if (showDelete) {
+		operationDiv.append(deleteButton);
+		deleteButton.click(function() {
+			deleteFile(filePath, listItemDiv);
+		});
+	}
 }
 
 /**
@@ -146,3 +151,59 @@ function download(path) {
 	}
 	window.open(downloadUrl);
 }
+
+/**
+ * 删除文件
+ * 
+ * @param path
+ */
+function deleteFile(path, listItemDiv) {
+	var deleteUrl = "file/delete?filePath=" + path;
+	// 如果链接中指定路径,则表示请求的绝对路径
+	if (filePathParameter) {
+		deleteUrl += "&isAbsolutePath=true";
+	}
+	$.ajax({
+		type : "get",
+		url : deleteUrl,
+		success : function(data) {
+			listItemDiv.remove();
+		},
+		error : function(XMLHttpRequest) {
+			showErrorMessage(XMLHttpRequest);
+		}
+	});
+}
+
+/**
+ * 显示请求返回的错误信息
+ * 
+ * @param XMLHttpRequest
+ */
+function showErrorMessage(XMLHttpRequest) {
+	// 处理后台传输的自定义的换行标志
+	var result = JSON.parse(XMLHttpRequest.responseText);
+	var message = result.message;
+	var detailedMessage = result.detailedMessage;
+	showErrorMessage(message, detailedMessage);
+}
+
+/**
+ * 显示错误信息
+ * 
+ * @param message
+ *            简要错误信息
+ * @param detailedMessage
+ *            详细错误信息
+ */
+function showErrorMessage(message, detailedMessage) {
+	$("#errorMessageContainer").removeAttr("hidden");
+	$("#message").html(message);
+	if (detailedMessage) {
+		$("#detailedMessageButton").removeAttr("hidden");
+	}
+	$("#detailedMessageButton").click(function() {
+		$("#detailedMessage").html(detailedMessage);
+		$("#detailedMessage").removeAttr("hidden");
+	});
+}

+ 15 - 0
src/main/webapp/resources/js/preview/app.js

@@ -464,6 +464,21 @@ function changeTextOfSelectScale() {
 	hiddenOption.hidden = true;
 }
 
+/**
+ * 下载报表
+ * 
+ * @param exportFileType
+ *            导出文件的格式
+ * @returns {String} 下载链接
+ */
+function downloadUrl(exportFileType) {
+	var downloadUrl = "print/export" + window.location.search;
+	exportFileType = exportFileType || "pdf";
+	// 导出文件的格式
+	downloadUrl += "&exportFileType=" + exportFileType;
+	return downloadUrl;
+}
+
 /**
  * 显示正在载入的动画
  */

+ 0 - 129
src/main/webapp/resources/js/preview/utils.js

@@ -1,129 +0,0 @@
-/**
- * 休眠一段时间
- * 
- * @param millsecond
- *            休眠时长(毫秒)
- */
-function sleep(millsecond) {
-	for (var d = Date.now(); Date.now() - d <= millsecond;)
-		;
-}
-
-/**
- * 下载报表
- * 
- * @param exportFileType
- *            导出文件的格式
- * @returns {String} 下载链接
- */
-function downloadUrl(exportFileType) {
-	var downloadUrl = "print/export" + window.location.search;
-	exportFileType = exportFileType || "pdf";
-	// 导出文件的格式
-	downloadUrl += "&exportFileType=" + exportFileType;
-	return downloadUrl;
-}
-
-/**
- * 获取链接参数
- * 
- * @param key
- *            参数的键
- * @returns 参数的值
- */
-function getParameter(key) {
-	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
-	var r = window.location.search.substr(1).match(reg);
-	if (r != null)
-		return decodeURI(r[2]);
-	return null;
-};
-
-/**
- * 获取所有参数
- * 
- * @returns {String}
- */
-function getParameters() {
-	var userName = getParameter("userName");
-	var reportName = getParameter("reportName");
-	var whereCondition = getParameter("whereCondition");
-	var otherParameters = getParameter("otherParameters");
-	pageIndex = getParameter("pageIndex") || 1;
-
-	var parameters = '?';
-	if (userName) {
-		parameters += 'userName=' + userName + '&';
-	}
-	if (reportName) {
-		parameters += 'reportName=' + reportName + '&';
-	}
-	if (whereCondition) {
-		parameters += 'whereCondition=' + whereCondition + '&';
-	}
-	if (otherParameters) {
-		parameters += 'otherParameters=' + otherParameters + '&';
-	}
-	if (pageIndex) {
-		parameters += 'pageIndex=' + pageIndex + '&';
-	}
-	return parameters;
-}
-
-// base64 解码
-function base64_decode(str) {
-	var c1, c2, c3, c4;
-	var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62,
-			-1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1,
-			-1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
-			15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1,
-			26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
-			43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
-	var i = 0, len = str.length, string = '';
-
-	while (i < len) {
-		do {
-			c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
-		} while (i < len && c1 == -1);
-
-		if (c1 == -1)
-			break;
-
-		do {
-			c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
-		} while (i < len && c2 == -1);
-
-		if (c2 == -1)
-			break;
-
-		string += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
-
-		do {
-			c3 = str.charCodeAt(i++) & 0xff;
-			if (c3 == 61)
-				return string;
-
-			c3 = base64DecodeChars[c3]
-		} while (i < len && c3 == -1);
-
-		if (c3 == -1)
-			break;
-
-		string += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
-
-		do {
-			c4 = str.charCodeAt(i++) & 0xff;
-			if (c4 == 61)
-				return string;
-			c4 = base64DecodeChars[c4]
-		} while (i < len && c4 == -1);
-
-		if (c4 == -1)
-			break;
-
-		string += String.fromCharCode(((c3 & 0x03) << 6) | c4)
-	}
-	return string;
-}

+ 27 - 1
src/main/webapp/resources/js/util/utils.js

@@ -1,4 +1,30 @@
-//对Date的扩展,将 Date 转化为指定格式的String
+/**
+ * 获取链接参数
+ * 
+ * @param key
+ *            参数的键
+ * @returns 参数的值
+ */
+function getParameter(key) {
+	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
+	var r = window.location.search.substr(1).match(reg);
+	if (r != null)
+		return decodeURI(r[2]);
+	return null;
+};
+
+/**
+ * 休眠一段时间
+ * 
+ * @param millsecond
+ *            休眠时长(毫秒)
+ */
+function sleep(millsecond) {
+	for (var d = Date.now(); Date.now() - d <= millsecond;)
+		;
+}
+
+// 对Date的扩展,将 Date 转化为指定格式的String
 Date.prototype.format = function(fmt) { // author: meizz
 	if (!fmt) {
 		// 默认格式