|
|
@@ -5,18 +5,25 @@ import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
+import java.net.URI;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.commons.lang.ArrayUtils;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
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.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.report.core.exception.ReportException;
|
|
|
import com.uas.report.service.FileService;
|
|
|
-import com.uas.report.support.JasperserverRestAPIConf;
|
|
|
+import com.uas.report.support.SysConf;
|
|
|
+import com.uas.report.util.FileUtils;
|
|
|
import com.uas.report.util.ReportUtils;
|
|
|
import com.uas.report.util.ZipUtils;
|
|
|
|
|
|
@@ -24,10 +31,38 @@ import com.uas.report.util.ZipUtils;
|
|
|
public class FileServiceImpl implements FileService {
|
|
|
|
|
|
@Autowired
|
|
|
- private JasperserverRestAPIConf jsRestAPIConf;
|
|
|
+ private SysConf sysConf;
|
|
|
|
|
|
private Logger logger = Logger.getLogger(getClass());
|
|
|
|
|
|
+ @Override
|
|
|
+ public String autoDeploy(String userNames) {
|
|
|
+ if (StringUtils.isEmpty(userNames)) {
|
|
|
+ throw new ReportException("参数不能为空:userNames");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 获取标准模板数据
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(IOUtils.toString(HttpClients.createDefault()
|
|
|
+ .execute(new HttpGet(URI.create(sysConf.getStandardJrxmlsUrl()))).getEntity().getContent()));
|
|
|
+ byte[] data = jsonObject.getBytes("data");
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ return "已自动部署:" + userNames;
|
|
|
+ } catch (UnsupportedOperationException | IOException e) {
|
|
|
+ throw new ReportException(e).setDetailedMessage(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public String upload(final String userName, String fileType, MultipartFile file) {
|
|
|
String message = "";
|
|
|
@@ -46,7 +81,7 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
- StringBuilder stringBuilder = new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/");
|
|
|
+ StringBuilder stringBuilder = new StringBuilder(sysConf.getLocalBaseDir()).append("/");
|
|
|
// jrxml模板和图片分别放在jrxml和Picture文件夹下,其他资源放在当前账套根路径下
|
|
|
if (fileType.equals("jrxml")) {
|
|
|
stringBuilder.append(userName).append("/").append("jrxml").append("/");
|
|
|
@@ -89,14 +124,39 @@ public class FileServiceImpl implements FileService {
|
|
|
@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();
|
|
|
+ return new StringBuilder(sysConf.getLocalBaseDir()).append("/").append(userName).append("/")
|
|
|
+ .append(sysConf.getLocalJrxmlDir()).append("/").append(reportName).append(".jrxml").toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @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);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getZip(String userName) {
|
|
|
// 账套路径
|
|
|
- String folderPath = jsRestAPIConf.getLocalBaseDir() + "/" + userName;
|
|
|
+ String folderPath = sysConf.getLocalBaseDir() + "/" + userName;
|
|
|
// 压缩后的压缩包路径,与账套在同一级
|
|
|
String zipFilePath = folderPath + ".zip";
|
|
|
ZipUtils.zipFolder(folderPath, zipFilePath);
|
|
|
@@ -135,7 +195,7 @@ public class FileServiceImpl implements FileService {
|
|
|
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(fileRelativePath)) {
|
|
|
throw new ReportException("参数不能为空:userName,fileRelativePath");
|
|
|
}
|
|
|
- return delete(new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/").append(userName).append("/")
|
|
|
+ return delete(new StringBuilder(sysConf.getLocalBaseDir()).append("/").append(userName).append("/")
|
|
|
.append(fileRelativePath).toString());
|
|
|
}
|
|
|
|
|
|
@@ -148,28 +208,8 @@ public class FileServiceImpl implements FileService {
|
|
|
if (!file.exists()) {
|
|
|
throw new ReportException("文件不存在,不必删除:" + filePath);
|
|
|
}
|
|
|
- delete(file);
|
|
|
+ FileUtils.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());
|
|
|
- }
|
|
|
-
|
|
|
}
|