|
|
@@ -3,6 +3,7 @@ package com.uas.report.service.impl;
|
|
|
import java.io.File;
|
|
|
import java.io.FileFilter;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
@@ -29,12 +30,12 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.lowagie.text.pdf.PdfReader;
|
|
|
import com.uas.report.SpecialProperties;
|
|
|
-import com.uas.report.core.exception.ReportException;
|
|
|
import com.uas.report.schedule.model.TaskInformation;
|
|
|
import com.uas.report.schedule.service.Executable;
|
|
|
import com.uas.report.schedule.service.TaskService;
|
|
|
import com.uas.report.service.FileService;
|
|
|
import com.uas.report.util.ArrayUtils;
|
|
|
+import com.uas.report.util.ExceptionUtils;
|
|
|
import com.uas.report.util.FileUtils;
|
|
|
import com.uas.report.util.ReportConstants;
|
|
|
import com.uas.report.util.ReportUtils;
|
|
|
@@ -82,36 +83,32 @@ public class FileServiceImpl implements FileService {
|
|
|
};
|
|
|
|
|
|
@Override
|
|
|
- public String autoDeploy(String sourceUserName, String destinationUserNames) {
|
|
|
+ public String autoDeploy(String sourceUserName, String destinationUserNames) throws IOException {
|
|
|
if (StringUtils.isEmpty(sourceUserName) || StringUtils.isEmpty(destinationUserNames)) {
|
|
|
- throw new ReportException("参数不能为空:sourceUserName,destinationUserNames");
|
|
|
+ throw new IllegalArgumentException("参数不能为空:sourceUserName,destinationUserNames");
|
|
|
}
|
|
|
- try {
|
|
|
- byte[] data = null;
|
|
|
- String stantardJrxmlsUrl = String.format(specialProperties.getStandardJrxmlsUrl(), sourceUserName);
|
|
|
- // 如果本机提供标准模板下载,直接从本地获取数据
|
|
|
- if (specialProperties.hasStandardJrxmls()) {
|
|
|
- data = getStandardJrxmls(sourceUserName);
|
|
|
- }
|
|
|
- // 本机没有标准模板,则先下载标准模板数据
|
|
|
- else {
|
|
|
- logger.info("get standardJrxmls from " + stantardJrxmlsUrl + "...");
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(IOUtils.toString(HttpClients.createDefault()
|
|
|
- .execute(new HttpGet(URI.create(stantardJrxmlsUrl))).getEntity().getContent()));
|
|
|
- data = jsonObject.getBytes("data");
|
|
|
- }
|
|
|
- if (ArrayUtils.isEmpty(data)) {
|
|
|
- throw new ReportException("标准模板不存在");
|
|
|
- }
|
|
|
- String[] userNameArray = destinationUserNames.split(",");
|
|
|
- // 创建账套路径并解压模板zip包
|
|
|
- for (String userName : userNameArray) {
|
|
|
- ZipUtils.unzip(data, getMasterPath(userName));
|
|
|
- }
|
|
|
- return "已自动部署:" + destinationUserNames;
|
|
|
- } catch (UnsupportedOperationException | IOException e) {
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
+ byte[] data = null;
|
|
|
+ String stantardJrxmlsUrl = String.format(specialProperties.getStandardJrxmlsUrl(), sourceUserName);
|
|
|
+ // 如果本机提供标准模板下载,直接从本地获取数据
|
|
|
+ if (specialProperties.hasStandardJrxmls()) {
|
|
|
+ data = getStandardJrxmls(sourceUserName);
|
|
|
+ }
|
|
|
+ // 本机没有标准模板,则先下载标准模板数据
|
|
|
+ else {
|
|
|
+ logger.info("get standardJrxmls from " + stantardJrxmlsUrl + "...");
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(IOUtils.toString(HttpClients.createDefault()
|
|
|
+ .execute(new HttpGet(URI.create(stantardJrxmlsUrl))).getEntity().getContent()));
|
|
|
+ data = jsonObject.getBytes("data");
|
|
|
}
|
|
|
+ if (ArrayUtils.isEmpty(data)) {
|
|
|
+ throw new IllegalStateException("标准模板不存在");
|
|
|
+ }
|
|
|
+ String[] userNameArray = destinationUserNames.split(",");
|
|
|
+ // 创建账套路径并解压模板zip包
|
|
|
+ for (String userName : userNameArray) {
|
|
|
+ ZipUtils.unzip(data, getMasterPath(userName));
|
|
|
+ }
|
|
|
+ return "已自动部署:" + destinationUserNames;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -134,7 +131,7 @@ public class FileServiceImpl implements FileService {
|
|
|
try {
|
|
|
ZipUtils.unzip(file.getBytes(), getMasterPath(userName));
|
|
|
} catch (IOException e) {
|
|
|
- throw new ReportException("文件解压失败").setDetailedMessage(e);
|
|
|
+ throw new IllegalStateException("文件解压失败\n" + ExceptionUtils.getDetailedMessage(e));
|
|
|
}
|
|
|
}
|
|
|
}).start();
|
|
|
@@ -151,7 +148,7 @@ public class FileServiceImpl implements FileService {
|
|
|
} else if (fileType.equals("other")) {
|
|
|
stringBuilder.append(userName).append("/");
|
|
|
} else {
|
|
|
- throw new ReportException("不支持上传该类型的文件:" + fileType);
|
|
|
+ throw new IllegalArgumentException("不支持上传该类型的文件:" + fileType);
|
|
|
}
|
|
|
|
|
|
stringBuilder.append(fileName);
|
|
|
@@ -163,14 +160,14 @@ public class FileServiceImpl implements FileService {
|
|
|
try {
|
|
|
FileUtils.write(targetFile.getAbsolutePath(), file.getBytes(), true);
|
|
|
return "成功上传文件至:" + targetFile.getPath();
|
|
|
- } catch (IllegalStateException | IOException | ReportException e) {
|
|
|
+ } catch (IllegalStateException | IOException e) {
|
|
|
logger.error("", e);
|
|
|
return "文件上传失败: " + fileName;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String upload(String filePath, Boolean isAbsolutePath, MultipartFile[] files) {
|
|
|
+ public String upload(String filePath, Boolean isAbsolutePath, MultipartFile[] files) throws IOException {
|
|
|
if (ArrayUtils.isEmpty(files)) {
|
|
|
return "文件为空,无法进行上传!";
|
|
|
}
|
|
|
@@ -182,22 +179,18 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
- try {
|
|
|
- for (MultipartFile file : files) {
|
|
|
- String fileName = file.getOriginalFilename();
|
|
|
- targetFile = new File(targetFile.getParent() + "/" + fileName);
|
|
|
- // 不能使用file.transferTo(targetFile),
|
|
|
- // 因为在spring boot下上传文件时会对临时路径进行处理,
|
|
|
- // 导致最终文件路径不正确,如果手动设置临时路径为根路径,
|
|
|
- // 又会因为权限问题导致文件写入失败,
|
|
|
- // 所以自己在指定路径创建文件,而不使用transferTo方法
|
|
|
- FileUtils.write(targetFile.getAbsolutePath(), file.getBytes(), true);
|
|
|
- stringBuilder.append("上传成功:").append(fileName).append("\n");
|
|
|
- }
|
|
|
- return stringBuilder.toString();
|
|
|
- } catch (IllegalStateException | IOException e) {
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ targetFile = new File(targetFile.getParent() + "/" + fileName);
|
|
|
+ // 不能使用file.transferTo(targetFile),
|
|
|
+ // 因为在spring boot下上传文件时会对临时路径进行处理,
|
|
|
+ // 导致最终文件路径不正确,如果手动设置临时路径为根路径,
|
|
|
+ // 又会因为权限问题导致文件写入失败,
|
|
|
+ // 所以自己在指定路径创建文件,而不使用transferTo方法
|
|
|
+ FileUtils.write(targetFile.getAbsolutePath(), file.getBytes(), true);
|
|
|
+ stringBuilder.append("上传成功:").append(fileName).append("\n");
|
|
|
}
|
|
|
+ return stringBuilder.toString();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -210,82 +203,78 @@ public class FileServiceImpl implements FileService {
|
|
|
@Override
|
|
|
public String getMasterPath(String userName) {
|
|
|
if (StringUtils.isEmpty(userName)) {
|
|
|
- throw new ReportException("参数不能为空:userName");
|
|
|
+ throw new IllegalArgumentException("参数不能为空:userName");
|
|
|
}
|
|
|
return new StringBuilder(specialProperties.getLocalBaseDir()).append("/").append(userName).toString();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public byte[] getStandardJrxmls(String userName) {
|
|
|
+ public byte[] getStandardJrxmls(String userName) throws IOException {
|
|
|
if (StringUtils.isEmpty(userName)) {
|
|
|
- throw new ReportException("未传入当前账套名称!");
|
|
|
+ throw new IllegalArgumentException("未传入当前账套名称!");
|
|
|
}
|
|
|
if (!specialProperties.hasStandardJrxmls()) {
|
|
|
- throw new ReportException("没有" + userName + "标准模板!");
|
|
|
+ throw new IllegalStateException("没有" + userName + "标准模板!");
|
|
|
}
|
|
|
try {
|
|
|
return ZipUtils.zipFolder(getMasterPath(specialProperties.getStandardMaster()) + "/" + userName,
|
|
|
FileServiceImpl.fileFilter);
|
|
|
} catch (Throwable e) {
|
|
|
- throw new ReportException("压缩失败").setDetailedMessage(e);
|
|
|
+ throw new IOException("压缩失败\n" + ExceptionUtils.getDetailedMessage(e));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void downloadStandardJrxmls(String userName, HttpServletResponse response) {
|
|
|
+ public void downloadStandardJrxmls(String userName, HttpServletResponse response) throws IOException {
|
|
|
if (StringUtils.isEmpty(userName)) {
|
|
|
- throw new ReportException("未传入当前账套名称!");
|
|
|
+ throw new IllegalArgumentException("未传入当前账套名称!");
|
|
|
}
|
|
|
if (!specialProperties.hasStandardJrxmls()) {
|
|
|
- throw new ReportException("没有" + userName + "标准模板!");
|
|
|
+ throw new IllegalStateException("没有" + userName + "标准模板!");
|
|
|
}
|
|
|
downloadZip(specialProperties.getStandardMaster() + "/" + userName, response);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void downloadZip(String userName, HttpServletResponse response) {
|
|
|
+ public void downloadZip(String userName, HttpServletResponse response) throws IOException {
|
|
|
if (StringUtils.isEmpty(userName)) {
|
|
|
- throw new ReportException("未传入当前账套名称!");
|
|
|
+ throw new IllegalArgumentException("未传入当前账套名称!");
|
|
|
}
|
|
|
String masterPath = getMasterPath(userName);
|
|
|
try {
|
|
|
byte[] data = ZipUtils.zipFolder(masterPath, FileServiceImpl.fileFilter);
|
|
|
download(data, new File(masterPath).getName() + ".zip", response);
|
|
|
} catch (Throwable e) {
|
|
|
- throw new ReportException("压缩失败").setDetailedMessage(e);
|
|
|
+ throw new IOException("压缩失败\n" + ExceptionUtils.getDetailedMessage(e));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void download(String filePath, Boolean isAbsolutePath, HttpServletResponse response) {
|
|
|
+ public void download(String filePath, Boolean isAbsolutePath, HttpServletResponse response) throws IOException {
|
|
|
if (StringUtils.isEmpty(filePath) || response == null) {
|
|
|
- throw new ReportException("参数不能为空:filePath,response");
|
|
|
+ throw new IllegalArgumentException("参数不能为空:filePath,response");
|
|
|
}
|
|
|
filePath = getAbsolutePath(filePath, isAbsolutePath);
|
|
|
File file = new File(filePath);
|
|
|
if (!file.exists()) {
|
|
|
- throw new ReportException("文件不存在:" + filePath);
|
|
|
+ throw new FileNotFoundException("文件不存在:" + filePath);
|
|
|
}
|
|
|
|
|
|
byte[] data = null;
|
|
|
String fileName = "";
|
|
|
// 下载文件夹之前,需进行压缩
|
|
|
- try {
|
|
|
- if (file.isDirectory()) {
|
|
|
- fileName = file.getName() + ".zip";
|
|
|
- data = ZipUtils.zipFolder(filePath, fileFilter);
|
|
|
- } else {
|
|
|
- fileName = file.getName();
|
|
|
- InputStream inputStream = new FileInputStream(file);
|
|
|
- data = new byte[inputStream.available()];
|
|
|
- inputStream.read(data);
|
|
|
- inputStream.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ReportException(e.getMessage()).setDetailedMessage(e);
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ fileName = file.getName() + ".zip";
|
|
|
+ data = ZipUtils.zipFolder(filePath, fileFilter);
|
|
|
+ } else {
|
|
|
+ fileName = file.getName();
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ data = new byte[inputStream.available()];
|
|
|
+ inputStream.read(data);
|
|
|
+ inputStream.close();
|
|
|
}
|
|
|
if (ArrayUtils.isEmpty(data)) {
|
|
|
- throw new ReportException("下载失败:" + filePath);
|
|
|
+ throw new IOException("下载失败:" + filePath);
|
|
|
}
|
|
|
download(data, fileName, response);
|
|
|
}
|
|
|
@@ -293,7 +282,7 @@ public class FileServiceImpl implements FileService {
|
|
|
@Override
|
|
|
public void download(byte[] data, String fileName, HttpServletResponse response) {
|
|
|
if (ArrayUtils.isEmpty(data) || StringUtils.isEmpty(fileName) || response == null) {
|
|
|
- throw new ReportException("参数不能为空:data,filePath,response");
|
|
|
+ throw new IllegalArgumentException("参数不能为空:data,filePath,response");
|
|
|
}
|
|
|
try {
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
@@ -307,30 +296,30 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String delete(String filePath, Boolean isAbsolutePath) {
|
|
|
+ public String delete(String filePath, Boolean isAbsolutePath) throws IOException {
|
|
|
// 路径不能为空,不能只包含'/'(根路径),不能含有'..'(不允许通过该方式删除上一级)
|
|
|
if (StringUtils.isEmpty(filePath) || StringUtils.isEmpty(filePath.replaceAll("/", ""))
|
|
|
|| filePath.contains("..")) {
|
|
|
- throw new ReportException("路径不合法:" + filePath);
|
|
|
+ throw new IllegalArgumentException("路径不合法:" + filePath);
|
|
|
}
|
|
|
filePath = getAbsolutePath(filePath, isAbsolutePath);
|
|
|
if (StringUtils.isEmpty(filePath)) {
|
|
|
- throw new ReportException("参数不能为空:filePath");
|
|
|
+ throw new IllegalArgumentException("参数不能为空:filePath");
|
|
|
}
|
|
|
File file = new File(filePath);
|
|
|
if (!file.exists()) {
|
|
|
- throw new ReportException("文件不存在,不必删除:" + filePath);
|
|
|
+ throw new IOException("文件不存在,不必删除:" + filePath);
|
|
|
}
|
|
|
FileUtils.delete(file);
|
|
|
return filePath;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Map<String, Object>> listFiles(String filePath, Boolean isAbsolutePath) {
|
|
|
+ public List<Map<String, Object>> listFiles(String filePath, Boolean isAbsolutePath) throws IOException {
|
|
|
filePath = getAbsolutePath(filePath, isAbsolutePath);
|
|
|
File file = new File(filePath);
|
|
|
if (!file.exists()) {
|
|
|
- throw new ReportException("文件不存在:" + filePath);
|
|
|
+ throw new FileNotFoundException("文件不存在:" + filePath);
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
|
@@ -462,12 +451,8 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public int getPageSize(String pdfFileAbsolutePath) {
|
|
|
- try {
|
|
|
- return new PdfReader(pdfFileAbsolutePath).getNumberOfPages();
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
- }
|
|
|
+ public int getPageSize(String pdfFileAbsolutePath) throws IOException {
|
|
|
+ return new PdfReader(pdfFileAbsolutePath).getNumberOfPages();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -478,10 +463,11 @@ public class FileServiceImpl implements FileService {
|
|
|
* @param isAbsolutePath
|
|
|
* 是否为绝对路径
|
|
|
* @return 绝对路径
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
- private String getAbsolutePath(String filePath, Boolean isAbsolutePath) {
|
|
|
+ private String getAbsolutePath(String filePath, Boolean isAbsolutePath) throws IOException {
|
|
|
if (StringUtils.isEmpty(filePath)) {
|
|
|
- throw new ReportException("路径不合法:" + filePath);
|
|
|
+ throw new IllegalArgumentException("路径不合法:" + filePath);
|
|
|
}
|
|
|
// 不是绝对路径的话,则相对于模板根路径
|
|
|
if (isAbsolutePath == null || !isAbsolutePath) {
|