sunyj 9 rokov pred
rodič
commit
abc9392863

+ 31 - 12
src/main/java/com/uas/report/controller/DownloadController.java → src/main/java/com/uas/report/controller/FileController.java

@@ -6,22 +6,30 @@ 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 org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.multipart.MultipartFile;
 
 import com.uas.report.core.exception.ReportException;
-import com.uas.report.service.DownloadService;
+import com.uas.report.service.FileService;
 
 /**
- * 下载请求
+ * 文件操作请求
  * 
  * @author sunyj
  * @since 2016年11月2日 下午2:05:04
  */
 @Controller
-@RequestMapping("/download")
-public class DownloadController {
+@RequestMapping("/file")
+public class FileController {
 
 	@Autowired
-	private DownloadService downloadService;
+	private FileService fileService;
+
+	@RequestMapping("/upload")
+	@ResponseBody
+	public String upload(String userName, String fileType, MultipartFile file) {
+		return fileService.upload(userName, fileType, file);
+	}
 
 	/**
 	 * 下载文件
@@ -30,9 +38,9 @@ public class DownloadController {
 	 *            文件路径
 	 * @param response
 	 */
-	@RequestMapping("")
+	@RequestMapping("/download")
 	public void download(String filePath, HttpServletResponse response) {
-		downloadService.download(filePath, response);
+		fileService.download(filePath, response);
 	}
 
 	/**
@@ -42,16 +50,16 @@ public class DownloadController {
 	 *            账套名
 	 * @param response
 	 */
-	@RequestMapping("/zip")
+	@RequestMapping("/download/zip")
 	public void downloadZip(String userName, HttpServletResponse response) {
 		if (StringUtils.isEmpty(userName)) {
 			throw new ReportException("未传入当前账套用户名!");
 		}
-		String zipFilePath = downloadService.getZip(userName);
+		String zipFilePath = fileService.getZip(userName);
 		if (zipFilePath.isEmpty()) {
 			throw new ReportException("压缩失败");
 		}
-		downloadService.download(zipFilePath, response);
+		fileService.download(zipFilePath, response);
 	}
 
 	/**
@@ -63,9 +71,20 @@ public class DownloadController {
 	 *            模板名称
 	 * @param response
 	 */
-	@RequestMapping("/jrxml")
+	@RequestMapping("/download/jrxml")
 	public void downloadJrxml(String userName, String reportName, HttpServletResponse response) {
-		downloadService.download(downloadService.getJrxml(userName, reportName), response);
+		fileService.download(fileService.getJrxml(userName, reportName), response);
+	}
+
+	@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);
+		}
 	}
 
 }

+ 0 - 93
src/main/java/com/uas/report/controller/UploadController.java

@@ -1,93 +0,0 @@
-package com.uas.report.controller;
-
-import java.io.File;
-import java.io.IOException;
-
-import org.apache.log4j.Logger;
-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 org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.multipart.MultipartFile;
-
-import com.uas.report.support.JasperserverRestAPIConf;
-import com.uas.report.util.ZipUtils;
-
-@Controller
-@RequestMapping("/upload")
-public class UploadController {
-	@Autowired
-	private JasperserverRestAPIConf jsRestAPIConf;
-
-	private Logger logger = Logger.getLogger(getClass());
-
-	/**
-	 * 上传文件
-	 * 
-	 * @param userName
-	 *            账套名,文件上传至相应账套路径下
-	 * @param file
-	 *            上传的文件
-	 * @return
-	 */
-	@RequestMapping("")
-	@ResponseBody
-	public String upload(final String userName, String fileType, MultipartFile file) {
-		String message = "";
-		if (StringUtils.isEmpty(userName)) {
-			message = "未传入当前账套用户名!";
-			logger.error(message);
-			return message;
-		}
-		if (file == null || file.isEmpty()) {
-			message = "文件为空,无法进行上传!";
-			logger.error(message);
-			return message;
-		}
-		if (StringUtils.isEmpty(fileType)) {
-			fileType = "jrxml";
-		}
-
-		String fileName = file.getOriginalFilename();
-		StringBuilder stringBuilder = new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/");
-		// jrxml模板和图片分别放在jrxml和Picture文件夹下,其他资源放在当前账套根路径下
-		if (fileType.equals("jrxml")) {
-			stringBuilder.append(userName).append("/").append("jrxml").append("/");
-		} else if (fileType.equals("image")) {
-			stringBuilder.append(userName).append("/").append("Picture").append("/");
-		} else if (fileType.equals("other")) {
-			stringBuilder.append(userName).append("/");
-		}
-
-		stringBuilder.append(fileName);
-		String targetFilePath = stringBuilder.toString();
-		final File targetFile = new File(targetFilePath);
-		if (!targetFile.exists()) {
-			targetFile.mkdirs();
-		}
-		try {
-			file.transferTo(targetFile);
-			message = "成功上传文件至:" + targetFile.getPath();
-			logger.info(message);
-			// 如果上传的是模板zip包,将其解压到相应的账套下
-			if (fileType.equals("zip")) {
-				new Thread(new Runnable() {
-					@Override
-					public void run() {
-						ZipUtils.unzip(targetFile.getPath(),
-								new File(targetFile.getPath()).getParent() + File.separator + userName);
-					}
-				}).start();
-			}
-			return message;
-		} catch (IllegalStateException | IOException e) {
-			e.printStackTrace();
-		}
-
-		message = "上传文件失败: " + fileName;
-		logger.error(message);
-		return message;
-	}
-
-}

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

@@ -1,41 +0,0 @@
-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);
-}

+ 74 - 0
src/main/java/com/uas/report/service/FileService.java

@@ -0,0 +1,74 @@
+package com.uas.report.service;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * 文件操作
+ * 
+ * @author sunyj
+ * @since 2016年11月2日 下午2:36:46
+ */
+public interface FileService {
+
+	/**
+	 * 上传文件
+	 * 
+	 * @param userName
+	 *            账套名,文件上传至相应账套路径下
+	 * @param file
+	 *            上传的文件
+	 * @return 上传结果
+	 */
+	public String upload(String userName, String fileType, MultipartFile file);
+
+	/**
+	 * 获取账套下的某个模板
+	 * 
+	 * @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);
+
+	/**
+	 * 账套下的某个文件
+	 * 
+	 * @param userName
+	 *            账套名称
+	 * @param fileRelativePath
+	 *            文件在账套下的相对路径
+	 * @return 文件路径
+	 */
+	public String delete(String userName, String fileRelativePath);
+
+	/**
+	 * 删除文件
+	 * 
+	 * @param filePath
+	 *            文件路径
+	 * @return 文件路径
+	 */
+	public String delete(String filePath);
+}

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

@@ -1,72 +0,0 @@
-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();
-		}
-	}
-
-}

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

@@ -0,0 +1,175 @@
+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.apache.log4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.alibaba.druid.util.StringUtils;
+import com.uas.report.core.exception.ReportException;
+import com.uas.report.service.FileService;
+import com.uas.report.support.JasperserverRestAPIConf;
+import com.uas.report.util.ReportUtils;
+import com.uas.report.util.ZipUtils;
+
+@Service
+public class FileServiceImpl implements FileService {
+
+	@Autowired
+	private JasperserverRestAPIConf jsRestAPIConf;
+
+	private Logger logger = Logger.getLogger(getClass());
+
+	@Override
+	public String upload(final String userName, String fileType, MultipartFile file) {
+		String message = "";
+		if (StringUtils.isEmpty(userName)) {
+			message = "未传入当前账套用户名!";
+			logger.error(message);
+			return message;
+		}
+		if (file == null || file.isEmpty()) {
+			message = "文件为空,无法进行上传!";
+			logger.error(message);
+			return message;
+		}
+		if (StringUtils.isEmpty(fileType)) {
+			fileType = "jrxml";
+		}
+
+		String fileName = file.getOriginalFilename();
+		StringBuilder stringBuilder = new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/");
+		// jrxml模板和图片分别放在jrxml和Picture文件夹下,其他资源放在当前账套根路径下
+		if (fileType.equals("jrxml")) {
+			stringBuilder.append(userName).append("/").append("jrxml").append("/");
+		} else if (fileType.equals("image")) {
+			stringBuilder.append(userName).append("/").append("Picture").append("/");
+		} else if (fileType.equals("other")) {
+			stringBuilder.append(userName).append("/");
+		}
+
+		stringBuilder.append(fileName);
+		String targetFilePath = stringBuilder.toString();
+		final File targetFile = new File(targetFilePath);
+		if (!targetFile.exists()) {
+			targetFile.mkdirs();
+		}
+		try {
+			file.transferTo(targetFile);
+			message = "成功上传文件至:" + targetFile.getPath();
+			logger.info(message);
+			// 如果上传的是模板zip包,将其解压到相应的账套下
+			if (fileType.equals("zip")) {
+				new Thread(new Runnable() {
+					@Override
+					public void run() {
+						ZipUtils.unzip(targetFile.getPath(),
+								new File(targetFile.getPath()).getParent() + File.separator + userName);
+					}
+				}).start();
+			}
+			return message;
+		} catch (IllegalStateException | IOException e) {
+			e.printStackTrace();
+		}
+
+		message = "上传文件失败: " + fileName;
+		logger.error(message);
+		return message;
+	}
+
+	@Override
+	public String getJrxml(String userName, String reportName) {
+		ReportUtils.checkParameters(userName, reportName);
+		return new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/").append(userName).append("/")
+				.append(jsRestAPIConf.getLocalJrxmlDir()).append("/").append(reportName).append(".jrxml").toString();
+	}
+
+	@Override
+	public String getZip(String userName) {
+		// 账套路径
+		String folderPath = jsRestAPIConf.getLocalBaseDir() + "/" + 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();
+		}
+	}
+
+	@Override
+	public String delete(String userName, String fileRelativePath) {
+		if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(fileRelativePath)) {
+			throw new ReportException("参数不能为空:userName,fileRelativePath");
+		}
+		return delete(new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/").append(userName).append("/")
+				.append(fileRelativePath).toString());
+	}
+
+	@Override
+	public String delete(String filePath) {
+		if (StringUtils.isEmpty(filePath)) {
+			throw new ReportException("参数不能为空:filePath");
+		}
+		File file = new File(filePath);
+		if (!file.exists()) {
+			throw new ReportException("文件不存在,不必删除:" + filePath);
+		}
+		delete(file);
+		return filePath;
+	}
+
+	/**
+	 * 递归删除文件(夹)
+	 * 
+	 * @param file
+	 *            文件(夹)
+	 */
+	private void delete(File file) {
+		if (file == null) {
+			return;
+		}
+		if (file.isDirectory()) {
+			File[] files = file.listFiles();
+			for (File f : files) {
+				delete(f);
+			}
+		}
+		file.delete();
+		logger.info("Deleted... " + file.getPath());
+	}
+
+}

+ 9 - 5
src/main/webapp/WEB-INF/views/index.html

@@ -37,19 +37,23 @@
 			</ol>
 		</div>
 		<div id="rightContainer">
-			<h2>2. 上传、下载</h2>
+			<h2>2. 上传、下载、删除</h2>
 			<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">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>
+					<u><li class="href">file/download?filePath=C:/sunyj/过程文档/sql/aq.sql</li></u>
+					<u><li class="href">file/download/zip?userName=UAS</li></u>
+					<u><li class="href">file/download/jrxml?userName=UAS&reportName=Purchase</li></u>
+				</ol>
+				<strong><li class="title1">删除</li></strong>
+				<ol>
+					<u><li class="href">file/delete?filePath=C:/test</li></u>
+					<u><li class="href">file/delete?userName=UAS&filePath=Picture/1.jpg</li></u>
 				</ol>
 			</ol>
 

+ 1 - 1
src/main/webapp/resources/js/upload/app.js

@@ -53,7 +53,7 @@ function upload() {
 	// $('#progressDiv').toggle();
 	// ajax异步上传
 	$.ajax({
-		url : "upload",
+		url : "file/upload",
 		type : "POST",
 		data : formData,
 		xhr : function() {