|
|
@@ -1,13 +1,20 @@
|
|
|
package com.uas.report.service.impl;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.FileFilter;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.URI;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
@@ -43,6 +50,7 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
@Override
|
|
|
public String autoDeploy(String userNames) {
|
|
|
+ logger.info("request... " + userNames);
|
|
|
if (StringUtils.isEmpty(userNames)) {
|
|
|
throw new ReportException("参数不能为空:userNames");
|
|
|
}
|
|
|
@@ -173,27 +181,48 @@ public class FileServiceImpl implements FileService {
|
|
|
String folderPath = getMasterPath(userName);
|
|
|
// 压缩后的压缩包路径,与账套在同一级
|
|
|
String zipFilePath = folderPath + ".zip";
|
|
|
- ZipUtils.zipFolder(folderPath, zipFilePath);
|
|
|
+ FileFilter fileFilter = new FileFilter() {
|
|
|
+ @Override
|
|
|
+ public boolean accept(File pathname) {
|
|
|
+ // 对zip、jasper文件不进行压缩
|
|
|
+ if (pathname.getPath().endsWith(".zip") || pathname.getPath().endsWith(".jasper")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ ZipUtils.zipFolder(folderPath, zipFilePath, fileFilter);
|
|
|
return zipFilePath;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void download(String filePath, HttpServletResponse response) {
|
|
|
+ public void download(String filePath, Boolean isAbsolutePath, HttpServletResponse response) {
|
|
|
if (StringUtils.isEmpty(filePath) || response == null) {
|
|
|
throw new ReportException("参数不能为空:filePath,response");
|
|
|
}
|
|
|
+ logger.info("request... " + filePath);
|
|
|
+ if (isAbsolutePath == null || !isAbsolutePath) {
|
|
|
+ filePath = sysConf.getLocalBaseDir() + "/" + filePath;
|
|
|
+ }
|
|
|
File file = new File(filePath);
|
|
|
if (!file.exists()) {
|
|
|
throw new ReportException("文件不存在:" + filePath);
|
|
|
}
|
|
|
- if (!file.isFile()) {
|
|
|
- throw new ReportException("并非文件:" + filePath);
|
|
|
+ // 下载文件夹之前,需进行压缩
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ String zipFilePath = getZip(getRelativePath(file));
|
|
|
+ if (zipFilePath.isEmpty()) {
|
|
|
+ throw new ReportException("压缩失败");
|
|
|
+ }
|
|
|
+ download(zipFilePath, true, response);
|
|
|
+ return;
|
|
|
}
|
|
|
try {
|
|
|
InputStream inputStream = new FileInputStream(file);
|
|
|
byte[] data = new byte[inputStream.available()];
|
|
|
inputStream.read(data);
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
|
|
|
+ response.setHeader("Content-Disposition",
|
|
|
+ "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
|
|
OutputStream outputStream = response.getOutputStream();
|
|
|
outputStream.write(data);
|
|
|
outputStream.flush();
|
|
|
@@ -214,6 +243,7 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
@Override
|
|
|
public String delete(String filePath) {
|
|
|
+ logger.info("request... " + filePath);
|
|
|
if (StringUtils.isEmpty(filePath)) {
|
|
|
throw new ReportException("参数不能为空:filePath");
|
|
|
}
|
|
|
@@ -225,6 +255,111 @@ public class FileServiceImpl implements FileService {
|
|
|
return filePath;
|
|
|
}
|
|
|
|
|
|
+ @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;
|
|
|
+ }
|
|
|
+ final File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ throw new ReportException("文件不存在:" + filePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
+ // 如果是文件,直接获取文件信息
|
|
|
+ if (file.isFile()) {
|
|
|
+ result.add(getFileInformation(file));
|
|
|
+ } else {
|
|
|
+ FileFilter fileFilter = new FileFilter() {
|
|
|
+ @Override
|
|
|
+ public boolean accept(File pathname) {
|
|
|
+ // 不显示zip压缩包、jasper文件的信息
|
|
|
+ if (pathname.getPath().endsWith(".zip") || pathname.getPath().endsWith(".jasper")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ File[] files = file.listFiles(fileFilter);
|
|
|
+ // 文件夹放在前面展示
|
|
|
+ List<File> directoryList = new ArrayList<>();
|
|
|
+ List<File> fileList = new ArrayList<>();
|
|
|
+ for (File f : files) {
|
|
|
+ if (f.isDirectory()) {
|
|
|
+ directoryList.add(f);
|
|
|
+ } else {
|
|
|
+ fileList.add(f);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result.addAll(getFileInformations(directoryList));
|
|
|
+ result.addAll(getFileInformations(fileList));
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取多个文件的信息
|
|
|
+ *
|
|
|
+ * @param files
|
|
|
+ * 文件
|
|
|
+ * @return 文件信息,包括name(String)、lastModified(String)、size(Long)、relativePath(
|
|
|
+ * String)、isDirectory(Boolean)
|
|
|
+ */
|
|
|
+ private List<Map<String, Object>> getFileInformations(List<File> files) {
|
|
|
+ List<Map<String, Object>> informationList = new ArrayList<>();
|
|
|
+ for (File file : files) {
|
|
|
+ informationList.add(getFileInformation(file));
|
|
|
+ }
|
|
|
+ return informationList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件信息
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * 文件
|
|
|
+ * @return 文件信息,包括name(String)、lastModified(String)、size(Long)、relativePath(
|
|
|
+ * String)、isDirectory(Boolean)
|
|
|
+ */
|
|
|
+ private Map<String, Object> getFileInformation(File file) {
|
|
|
+ if (file == null || !file.exists()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Map<String, Object> information = new HashMap<>();
|
|
|
+ information.put("name", file.getName());
|
|
|
+ information.put("relativePath", getRelativePath(file));
|
|
|
+ information.put("lastModified",
|
|
|
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified())));
|
|
|
+ if (file.isFile()) {
|
|
|
+ information.put("size", file.length());
|
|
|
+ information.put("isDirectory", false);
|
|
|
+ } else {
|
|
|
+ information.put("isDirectory", true);
|
|
|
+ }
|
|
|
+ return information;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件相对于本地资源根路径的路径
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * 文件
|
|
|
+ * @return 相对于本地资源根路径的路径
|
|
|
+ */
|
|
|
+ private String getRelativePath(File file) {
|
|
|
+ if (file == null || !file.exists()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 获取相对路径,须将本地资源根路径替换掉,并且文件分隔符统一使用 '/'
|
|
|
+ return file.getPath().replace(new File(sysConf.getLocalBaseDir()).getPath(), "").replace("\\", "/");
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public boolean isFileValid(String filePath, String jrxmlFilePath) {
|
|
|
if (!StringUtils.isEmpty(filePath) && !StringUtils.isEmpty(jrxmlFilePath)) {
|
|
|
@@ -297,4 +432,5 @@ public class FileServiceImpl implements FileService {
|
|
|
throw new ReportException(e).setDetailedMessage(e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|