提交 fc79f7c3 编写于 作者: zhouweidong's avatar zhouweidong

同步cloud批量下载文件逻辑

上级 c6bf0424
......@@ -5,8 +5,11 @@ import cn.ibizlab.core.disk.service.ISDFileService;
import cn.ibizlab.core.disk.extensions.vo.FileItem;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.errors.InternalServerErrorException;
import cn.ibizlab.util.helper.ZipUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -15,13 +18,11 @@ import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
......@@ -31,6 +32,7 @@ import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipOutputStream;
@Primary
@Slf4j
......@@ -158,6 +160,50 @@ public class DiskCoreService {
throw new InternalServerErrorException("文件未找到");
}
/**
* 批量下载文件 [{ "id":"fileid1"},{"id":"fileid2"}]
* @param strCat
* @param list
* @return
*/
public File getFile(String strCat, List<JsonNode> list) {
if (ObjectUtils.isEmpty(list)) {
throw new InternalServerErrorException("未传入文件清单");
}
List<File> fileList = new ArrayList<File>();
for (JsonNode item : list) {
if (item instanceof ObjectNode) {
ObjectNode map = (ObjectNode) item;
item = map.get("id");
}
fileList.add(this.getFile(strCat, item.asText()));
}
try {
File tempFile = File.createTempFile("oss", ".zip");
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(tempFile))) {
for (File file : fileList) {
if (file.getParentFile() == null || file.getParentFile().getParentFile() == null) {
throw new Exception("文件路径不正确");
}
int nFolderLength = file.getParentFile().getParentFile().getAbsolutePath().length() + 1;
long nTime = System.currentTimeMillis();
ZipUtils.zip(file, zipOutputStream, nFolderLength);
log.debug(String.format("压缩文件[%1$s]耗时[%2$s]ms", file.getAbsolutePath(), System.currentTimeMillis() - nTime));
}
zipOutputStream.flush();
zipOutputStream.close();
}
return tempFile;
} catch (Throwable ex) {
throw new InternalServerErrorException(String.format("生成压缩文件发生异常,%1$s", ex.getMessage()));
}
}
public File getFileById(String fileId) {
SDFile sdFile=sdFileService.getById(fileId);
if(sdFile!=null&&(!StringUtils.isEmpty(sdFile.getFilePath()))){
......
......@@ -8,6 +8,7 @@ import cn.ibizlab.core.disk.extensions.vo.FileItem;
import cn.ibizlab.core.disk.service.ISDFileService;
import cn.ibizlab.util.errors.BadRequestAlertException;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -101,6 +102,14 @@ public class DiskCoreResource
this.sendRespose(response, file);
}
@PostMapping(value = "net-disk/download/")
@ResponseStatus(HttpStatus.OK)
public void download(@RequestBody List<JsonNode> list, HttpServletRequest request, HttpServletResponse response){
File file= diskCoreService.getFile(null, list);
response.setHeader("Content-Disposition", "attachment;filename="+getFileName(request.getHeader("User-Agent"),file.getName()));
this.sendRespose(response, file);
}
@GetMapping(value = {"net-disk/openview/{folder}/{id}/{name}.{ext}","net-disk/files/{folder}/{id}/{name}.{ext}"})
@ResponseStatus(HttpStatus.OK)
public void open(@PathVariable("folder") String folder, @PathVariable("id") String id,
......
package cn.ibizlab.util.helper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(ZipUtils.class);
public static void zip(File file, File zipFile) throws IOException {
long nBeginTime = System.currentTimeMillis();
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) {
long nTime = System.currentTimeMillis();
zip(file, zipOutputStream);
log.debug(String.format("压缩文件[%1$s]耗时[%2$s]ms", file.getAbsolutePath(), System.currentTimeMillis() - nTime));
zipOutputStream.flush();
zipOutputStream.close();
}
log.debug(String.format("生成压缩文件[%1$s]耗时[%2$s]ms", zipFile.getAbsolutePath(), System.currentTimeMillis() - nBeginTime));
}
public static void zip(List<File> fileList, File zipFile) throws IOException {
long nBeginTime = System.currentTimeMillis();
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) {
for(File file : fileList) {
long nTime = System.currentTimeMillis();
zip(file, zipOutputStream);
log.debug(String.format("压缩文件[%1$s]耗时[%2$s]ms", file.getAbsolutePath(), System.currentTimeMillis() - nTime));
}
zipOutputStream.flush();
zipOutputStream.close();
}
log.debug(String.format("生成压缩文件[%1$s]耗时[%2$s]ms", zipFile.getAbsolutePath(), System.currentTimeMillis() - nBeginTime));
}
public static void zip(File file, ZipOutputStream zipOutputStream) throws IOException {
int nFolderLength = 0;
if (file.isFile()) {
nFolderLength = file.getParentFile().getAbsolutePath().length();
} else {
nFolderLength = file.getAbsolutePath().length();
}
zip(file, zipOutputStream, nFolderLength + 1);
}
public static void zip(File file, ZipOutputStream zipOutputStream, int nFolderLength) throws IOException {
if (file.isFile()) {
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
ZipEntry zipEntry = new ZipEntry(file.getAbsolutePath().substring(nFolderLength).replace("\\", "/"));
zipEntry.setTime(file.lastModified());
zipOutputStream.putNextEntry(zipEntry);
try (FileInputStream fis = new FileInputStream(file); FileChannel channel = fis.getChannel();) {
while (true) {
byteBuffer.clear();
int read = channel.read(byteBuffer);
if (read == -1)
break;
zipOutputStream.write(byteBuffer.array(), 0, read);
}
}
zipOutputStream.closeEntry();
} else {
File[] files = file.listFiles();
if (files == null || files.length == 0) {
ZipEntry zipEntry = new ZipEntry(file.getAbsolutePath().substring(nFolderLength).replace("\\", "/") + "/");
zipEntry.setTime(file.lastModified());
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.closeEntry();
} else {
for (File item : files) {
zip(item, zipOutputStream, nFolderLength);
}
}
}
}
public static void unzip(File zipFile, File dstFolder) throws IOException {
if (!dstFolder.exists()) {
dstFolder.mkdirs();
}
String destDir = dstFolder.getAbsolutePath();
// buffer to read and write data in the file
byte[] buffer = new byte[1024];
try(FileInputStream fis = new FileInputStream(zipFile); ZipInputStream zis = new ZipInputStream(fis); ) {
ZipEntry ZE = zis.getNextEntry();
while (ZE != null) {
String fileName = ZE.getName();
File newFile = new File(destDir + File.separator + fileName);
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
// close this ZipEntry
zis.closeEntry();
ZE = zis.getNextEntry();
}
}
}
public static void main(String[] args) {
try {
long nTime = System.currentTimeMillis();
ZipOutputStream zipOutputStream;
zipOutputStream = new ZipOutputStream(new FileOutputStream(new File("d:/aa.zip")));
zip(new File("I:\\code\\DEMO\\CBD466CC-A18A-47EC-A018-A6E7E5AB191F\\trunk\\TEST11\\model"), zipOutputStream);
zipOutputStream.flush();
zipOutputStream.close();
nTime = System.currentTimeMillis() - nTime;
System.out.print(String.format("压缩文件耗时[%1$s]ms", nTime));
nTime = System.currentTimeMillis();
unzip(new File("d:/aa.zip"), new File("d:/aaaa"));
nTime = System.currentTimeMillis() - nTime;
System.out.print(String.format("解压文件耗时[%1$s]ms", nTime));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册