|
|
@@ -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());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|