|
@@ -1,5 +1,7 @@
|
|
package com.qmth.distributed.print.business.service.impl;
|
|
package com.qmth.distributed.print.business.service.impl;
|
|
|
|
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
@@ -28,6 +30,7 @@ import org.springframework.stereotype.Service;
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -326,7 +329,7 @@ public class CommonServiceImpl implements CommonService {
|
|
JSONObject object = JSONObject.parseObject(path);
|
|
JSONObject object = JSONObject.parseObject(path);
|
|
String filePath = object.getString(SystemConstant.PATH);
|
|
String filePath = object.getString(SystemConstant.PATH);
|
|
String type = object.getString(SystemConstant.TYPE);
|
|
String type = object.getString(SystemConstant.TYPE);
|
|
- if(filePath.endsWith(".html")) {
|
|
|
|
|
|
+ if (filePath.endsWith(".html")) {
|
|
StringBuffer sb = new StringBuffer();
|
|
StringBuffer sb = new StringBuffer();
|
|
try {
|
|
try {
|
|
InputStream fis;
|
|
InputStream fis;
|
|
@@ -353,4 +356,89 @@ public class CommonServiceImpl implements CommonService {
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public File copyFile(String rootPath, BasicAttachment attachment) {
|
|
|
|
+ JSONObject object = JSONObject.parseObject(attachment.getPath());
|
|
|
|
+ String filePath = object.getString(SystemConstant.PATH);
|
|
|
|
+ String type = object.getString(SystemConstant.TYPE);
|
|
|
|
+ if (type.equals(SystemConstant.OSS)) {
|
|
|
|
+ File localPath = new File(rootPath, attachment.getName() + attachment.getType());
|
|
|
|
+ try {
|
|
|
|
+ File file = ossUtil.ossDownload(filePath, localPath.getPath());
|
|
|
|
+ return file;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("从OSS上下载文件失败");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("本地文件不存在");
|
|
|
|
+ }
|
|
|
|
+ return file;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void downloadFileAndZip(HttpServletResponse response, String rootPath, List<File> files) {
|
|
|
|
+ String schoolId = ServletUtil.getRequestHeaderSchoolId().toString();
|
|
|
|
+ long time = System.nanoTime();
|
|
|
|
+ File rootFile = new File(rootPath);
|
|
|
|
+ // 创建保存目录
|
|
|
|
+ if (!rootFile.exists()) {
|
|
|
|
+ rootFile.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ File zipFile = null;
|
|
|
|
+ try {
|
|
|
|
+ zipFile = FileUtil.file(SystemConstant.DOWNLOAD_TEMP + File.separator + schoolId, time + ".zip");
|
|
|
|
+ // 压缩文件
|
|
|
|
+ if (!zipFile.exists()) {
|
|
|
|
+ zipFile.createNewFile();
|
|
|
|
+ }
|
|
|
|
+ File[] srcFiles = files.toArray(new File[files.size()]);
|
|
|
|
+// ZipUtil.zip(rootFile.getAbsolutePath(), zipFile.getAbsolutePath(), false);
|
|
|
|
+
|
|
|
|
+ ZipUtil.zip(zipFile, false, srcFiles);
|
|
|
|
+ outputFile(response, zipFile, String.valueOf(time));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("下载失败");
|
|
|
|
+ } finally {
|
|
|
|
+ // 删除zip文件
|
|
|
|
+ FileUtil.del(zipFile);
|
|
|
|
+ // 删除压缩内容
|
|
|
|
+ FileUtil.del(rootPath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void outputFile(HttpServletResponse response, File file, String fileName) {
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ response.sendError(404, "File not found!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
|
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
|
+ int len = 0;
|
|
|
|
+
|
|
|
|
+ String fName = new String(fileName.getBytes(), "ISO-8859-1");
|
|
|
|
+
|
|
|
|
+ response.reset();
|
|
|
|
+ response.setContentType("application/x-msdownload");
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
|
|
+
|
|
|
|
+ OutputStream outStream = response.getOutputStream();
|
|
|
|
+
|
|
|
|
+ while ((len = br.read(buf)) > 0) {
|
|
|
|
+ outStream.write(buf, 0, len);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ br.close();
|
|
|
|
+ outStream.close();
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|