|
|
@@ -51,7 +51,7 @@ public class FileServiceImpl implements FileService {
|
|
|
/**
|
|
|
* 查看、压缩文件时,对文件进行过滤
|
|
|
*/
|
|
|
- private FileFilter fileFilter = new FileFilter() {
|
|
|
+ public static final FileFilter fileFilter = new FileFilter() {
|
|
|
@Override
|
|
|
public boolean accept(File file) {
|
|
|
if (file == null || !file.exists()) {
|
|
|
@@ -74,6 +74,7 @@ public class FileServiceImpl implements FileService {
|
|
|
throw new ReportException("参数不能为空:userNames");
|
|
|
}
|
|
|
try {
|
|
|
+ logger.info("get standardJrxmls from " + sysConf.getStandardJrxmlsUrl() + "...");
|
|
|
// 获取标准模板数据
|
|
|
JSONObject jsonObject = JSONObject.parseObject(IOUtils.toString(HttpClients.createDefault()
|
|
|
.execute(new HttpGet(URI.create(sysConf.getStandardJrxmlsUrl()))).getEntity().getContent()));
|
|
|
@@ -81,14 +82,10 @@ public class FileServiceImpl implements FileService {
|
|
|
if (ArrayUtils.isEmpty(data)) {
|
|
|
throw new ReportException("标准模板不存在");
|
|
|
}
|
|
|
- // 写入本地zip文件
|
|
|
- String filePath = new StringBuilder(sysConf.getLocalBaseDir()).append("/")
|
|
|
- .append(sysConf.getStandardMaster()).append(".zip").toString();
|
|
|
- FileUtils.create(filePath, data);
|
|
|
String[] userNameArray = userNames.split(",");
|
|
|
// 创建账套路径并解压模板zip包
|
|
|
for (String userName : userNameArray) {
|
|
|
- ZipUtils.unzip(filePath, sysConf.getLocalBaseDir() + "/" + userName);
|
|
|
+ ZipUtils.unzip(data, getMasterPath(userName));
|
|
|
}
|
|
|
return "已自动部署:" + userNames;
|
|
|
} catch (UnsupportedOperationException | IOException e) {
|
|
|
@@ -97,7 +94,7 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String upload(final String userName, String fileType, MultipartFile file) {
|
|
|
+ public String upload(final String userName, String fileType, final MultipartFile file) {
|
|
|
String message = "";
|
|
|
if (StringUtils.isEmpty(userName)) {
|
|
|
message = "未传入当前账套用户名!";
|
|
|
@@ -113,6 +110,23 @@ public class FileServiceImpl implements FileService {
|
|
|
fileType = "jrxml";
|
|
|
}
|
|
|
|
|
|
+ // 如果上传的是模板zip包,直接获取字节数据,将其解压到相应的账套下
|
|
|
+ if (fileType.equals("zip")) {
|
|
|
+ message = "文件上传成功";
|
|
|
+ logger.info(message);
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ ZipUtils.unzip(file.getBytes(), getMasterPath(userName));
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ReportException("文件解压失败").setDetailedMessage(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
StringBuilder stringBuilder = new StringBuilder(sysConf.getLocalBaseDir()).append("/");
|
|
|
// jrxml模板和图片分别放在jrxml和Picture文件夹下,其他资源放在当前账套根路径下
|
|
|
@@ -122,6 +136,8 @@ public class FileServiceImpl implements FileService {
|
|
|
stringBuilder.append(userName).append("/").append("Picture").append("/");
|
|
|
} else if (fileType.equals("other")) {
|
|
|
stringBuilder.append(userName).append("/");
|
|
|
+ } else {
|
|
|
+ throw new ReportException("不支持上传该类型的文件:" + fileType);
|
|
|
}
|
|
|
|
|
|
stringBuilder.append(fileName);
|
|
|
@@ -134,20 +150,10 @@ public class FileServiceImpl implements FileService {
|
|
|
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;
|
|
|
+ message = "文件上传失败: " + fileName;
|
|
|
logger.error(message);
|
|
|
return message;
|
|
|
}
|
|
|
@@ -180,7 +186,6 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
logger.info(message);
|
|
|
return message;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -199,38 +204,21 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public byte[] getStandardJrxmls() {
|
|
|
- // 压缩标准模板
|
|
|
- String zipFilePath = getZip(sysConf.getStandardMaster());
|
|
|
- if (zipFilePath.isEmpty()) {
|
|
|
- throw new ReportException("压缩失败");
|
|
|
- }
|
|
|
- File zipFile = new File(zipFilePath);
|
|
|
- if (!zipFile.exists()) {
|
|
|
- throw new ReportException("文件不存在:" + zipFilePath);
|
|
|
- }
|
|
|
- if (!zipFile.isFile()) {
|
|
|
- throw new ReportException("并非文件:" + zipFilePath);
|
|
|
- }
|
|
|
- try {
|
|
|
- InputStream inputStream = new FileInputStream(zipFile);
|
|
|
- byte[] data = new byte[inputStream.available()];
|
|
|
- inputStream.read(data);
|
|
|
- inputStream.close();
|
|
|
- return data;
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
- }
|
|
|
+ public void downloadStandardJrxmls(HttpServletResponse response) {
|
|
|
+ downloadZip(sysConf.getStandardMaster(), response);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String getZip(String userName) {
|
|
|
- // 账套路径
|
|
|
- String folderPath = getMasterPath(userName);
|
|
|
- // 压缩后的压缩包路径,与账套在同一级
|
|
|
- String zipFilePath = folderPath + ".zip";
|
|
|
- ZipUtils.zipFolder(folderPath, zipFilePath, fileFilter);
|
|
|
- return zipFilePath;
|
|
|
+ public void downloadZip(String userName, HttpServletResponse response) {
|
|
|
+ if (StringUtils.isEmpty(userName)) {
|
|
|
+ throw new ReportException("未传入当前账套用户名!");
|
|
|
+ }
|
|
|
+ String masterPath = getMasterPath(userName);
|
|
|
+ byte[] data = ZipUtils.zipFolder(masterPath, FileServiceImpl.fileFilter);
|
|
|
+ if (ArrayUtils.isEmpty(data)) {
|
|
|
+ throw new ReportException("压缩失败");
|
|
|
+ }
|
|
|
+ download(data, new File(masterPath).getName() + ".zip", response);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -244,25 +232,40 @@ public class FileServiceImpl implements FileService {
|
|
|
if (!file.exists()) {
|
|
|
throw new ReportException("文件不存在:" + filePath);
|
|
|
}
|
|
|
+
|
|
|
+ byte[] data = null;
|
|
|
+ String fileName = "";
|
|
|
// 下载文件夹之前,需进行压缩
|
|
|
if (file.isDirectory()) {
|
|
|
- String zipFilePath = getZip(getRelativePath(file));
|
|
|
- if (zipFilePath.isEmpty()) {
|
|
|
- throw new ReportException("压缩失败");
|
|
|
+ fileName = file.getName() + ".zip";
|
|
|
+ data = ZipUtils.zipFolder(filePath, fileFilter);
|
|
|
+ } else {
|
|
|
+ fileName = file.getName();
|
|
|
+ try {
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ data = new byte[inputStream.available()];
|
|
|
+ inputStream.read(data);
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- download(zipFilePath, true, response);
|
|
|
- return;
|
|
|
+ }
|
|
|
+ if (ArrayUtils.isEmpty(data)) {
|
|
|
+ throw new ReportException("下载失败:" + filePath);
|
|
|
+ }
|
|
|
+ download(data, fileName, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ @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");
|
|
|
}
|
|
|
try {
|
|
|
- InputStream inputStream = new FileInputStream(file);
|
|
|
- byte[] data = new byte[inputStream.available()];
|
|
|
- inputStream.read(data);
|
|
|
- response.setHeader("Content-Disposition",
|
|
|
- "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
OutputStream outputStream = response.getOutputStream();
|
|
|
outputStream.write(data);
|
|
|
outputStream.flush();
|
|
|
- inputStream.close();
|
|
|
outputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|