|
@@ -1,5 +1,8 @@
|
|
|
package com.caimei.service.data.impl;
|
|
|
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
import com.caimei.mapper.cmMapper.FileMapper;
|
|
|
import com.caimei.model.ResponseJson;
|
|
|
import com.caimei.model.vo.FileTreeVo;
|
|
@@ -17,7 +20,9 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
/**
|
|
@@ -48,32 +53,35 @@ public class DatabaseServiceImpl implements DatabaseService {
|
|
|
|
|
|
@Override
|
|
|
public void downLoadZip(Integer fileId, String packageName, HttpServletResponse response) {
|
|
|
- //1.查下一级所有文件
|
|
|
- List<FileTreeVo> sonFiles = fileMapper.findSonFiles(fileId);
|
|
|
- //2.有package则在服务器固定路径new file.mkdir
|
|
|
- StringBuilder filePath = new StringBuilder("/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/");
|
|
|
- filePath = filePath.append(packageName + "/");
|
|
|
+ //有package则在服务器固定路径new file.mkdir
|
|
|
+ UUID uuid = UUID.randomUUID();
|
|
|
+ String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/" + uuid + "/";
|
|
|
+ filePath = filePath + packageName;
|
|
|
+ String zipPath = filePath;
|
|
|
+ String delPath = filePath;
|
|
|
if ("dev".equals(active)) {
|
|
|
- filePath = new StringBuilder("D:\\caimei-workSpace\\file\\");
|
|
|
- filePath.append(packageName);
|
|
|
+ filePath = "D:\\caimei-workSpace\\file\\" + uuid + "\\";
|
|
|
+ zipPath = filePath + packageName;
|
|
|
+ delPath = filePath;
|
|
|
+ filePath = filePath + packageName + "\\";
|
|
|
}
|
|
|
- String zipPath = filePath.toString();
|
|
|
- File fil = new File(filePath.toString());
|
|
|
- fil.mkdirs();
|
|
|
+ boolean mkdirs = new File(filePath).mkdirs();
|
|
|
+ Assert.isTrue(mkdirs, "文件夹创建失败");
|
|
|
+
|
|
|
recursion(fileId, filePath);
|
|
|
//压缩
|
|
|
FileZipUtils.compress(zipPath, "", true);
|
|
|
String s = zipPath + ".zip";
|
|
|
- String fileName=packageName+".zip";
|
|
|
+ String fileName = packageName + ".zip";
|
|
|
File file = new File(s);
|
|
|
try {
|
|
|
- if(file.exists()){
|
|
|
+ if (file.exists()) {
|
|
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
ServletOutputStream outputStream = response.getOutputStream();
|
|
|
// 设置下载文件的mineType,告诉浏览器下载文件类型
|
|
|
- // int i = fileName.lastIndexOf(".");
|
|
|
- // String substring = fileName.substring(i + 1, filePath.length());
|
|
|
- // response.setContentType(substring);
|
|
|
+ // int i = fileName.lastIndexOf(".");
|
|
|
+ // String substring = fileName.substring(i + 1, filePath.length());
|
|
|
+ // response.setContentType(substring);
|
|
|
// 设置一个响应头,无论是否被浏览器解析,都下载
|
|
|
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
|
|
|
byte[] bytes = new byte[1024];
|
|
@@ -81,40 +89,75 @@ public class DatabaseServiceImpl implements DatabaseService {
|
|
|
while ((len = fileInputStream.read(bytes)) != -1) {
|
|
|
outputStream.write(bytes, 0, len);
|
|
|
}
|
|
|
+ fileInputStream.close();
|
|
|
+ outputStream.close();
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
+ FileZipUtils.deleteFile(new File(delPath));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void insertNewFile(String fileName, String filePath, String previewUrl,Integer parentId,String fileType) {
|
|
|
+ FileTreeVo fileTreeVo = new FileTreeVo();
|
|
|
+ fileTreeVo.setFileName(fileName);
|
|
|
+ fileTreeVo.setOssName(filePath);
|
|
|
+ fileTreeVo.setOssUrl(previewUrl);
|
|
|
+ fileTreeVo.setParentId(parentId);
|
|
|
+ fileTreeVo.setFileType(fileType);
|
|
|
+ fileMapper.insertNewFile(fileTreeVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<HashMap<String, String>> ossTokenGet() {
|
|
|
+ return ResponseJson.success(OSSUtils.getToken());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson getOssUploadResult(String fileName, String ossName, String ossUrl, String fileSize,Integer parentId) {
|
|
|
+ FileTreeVo fileTreeVo = new FileTreeVo();
|
|
|
+ fileTreeVo.setFileName(fileName);
|
|
|
+ fileTreeVo.setOssName(ossName);
|
|
|
+ fileTreeVo.setOssUrl(ossUrl);
|
|
|
+ fileTreeVo.setParentId(parentId);
|
|
|
+ fileTreeVo.setFileType(fileName.substring(fileName.indexOf(".")+1));
|
|
|
+ fileTreeVo.setFileSize(fileSize);
|
|
|
+ fileMapper.insertNewFile(fileTreeVo);
|
|
|
+ return ResponseJson.success();
|
|
|
}
|
|
|
- //todo 一级包会重复,recursion中filepath会重复
|
|
|
- private void recursion(Integer fileId, StringBuilder filePath) {
|
|
|
+
|
|
|
+ private void recursion(Integer fileId, String filePath) {
|
|
|
//1.如果当前目录存在普通文件,下载当前目录下的文件到文件夹中
|
|
|
+ String finalFilePath = filePath;
|
|
|
List<FileTreeVo> commonFile = existsPackageOrCommonFile(fileId, 2);
|
|
|
if (null != commonFile && commonFile.size() > 0) {
|
|
|
commonFile.forEach(c -> {
|
|
|
String fileName = "";
|
|
|
if ("dev".equals(active)) {
|
|
|
- fileName = filePath + "\\" + c.getFileName();
|
|
|
+ fileName = finalFilePath + "\\" + c.getFileName();
|
|
|
} else {
|
|
|
- fileName = filePath + "/" + c.getFileName();
|
|
|
+ fileName = finalFilePath + "/" + c.getFileName();
|
|
|
}
|
|
|
OSSUtils.downFileByFilePath("authFile/", c.getOssName(), fileName);
|
|
|
});
|
|
|
}
|
|
|
//2.如果存在文件夹循环文件夹,递归本方法
|
|
|
List<FileTreeVo> packages = existsPackageOrCommonFile(fileId, 1);
|
|
|
+ HashMap<Integer, String> sonPackage = new HashMap<Integer, String>();
|
|
|
if (null != packages && packages.size() > 0) {
|
|
|
for (FileTreeVo aPackage : packages) {
|
|
|
if ("dev".equals(active)) {
|
|
|
- filePath.append("\\" + aPackage.getFileName());
|
|
|
+ filePath = finalFilePath + "\\" + aPackage.getFileName();
|
|
|
} else {
|
|
|
- filePath.append(aPackage.getFileName() + "/");
|
|
|
+ filePath = finalFilePath + aPackage.getFileName() + "/";
|
|
|
}
|
|
|
File fil = new File(filePath.toString());
|
|
|
fil.mkdirs();
|
|
|
- recursion(aPackage.getId(), filePath);
|
|
|
+ sonPackage.put(aPackage.getId(), filePath);
|
|
|
}
|
|
|
}
|
|
|
+ sonPackage.forEach(this::recursion);
|
|
|
}
|
|
|
|
|
|
/**
|