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

批量下载文件去除目录,支持文件平铺展示

上级 6ce1236f
......@@ -28,10 +28,7 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.zip.ZipOutputStream;
@Primary
......@@ -204,6 +201,50 @@ public class DiskCoreService {
}
/**
* 批量压缩文件,压缩包内所有文件平铺展示,子目录文件重名时,通过序号区分(如:文件名.txt; 件名(1).txt )
* @param strCat
* @param list
* @return
*/
public File getFile2(String strCat, List<JsonNode> list) {
if (ObjectUtils.isEmpty(list)) {
throw new InternalServerErrorException("未传入文件清单");
}
List<File> fileList = new ArrayList<>();
for (JsonNode item : list) {
if (item instanceof ObjectNode) {
ObjectNode map = (ObjectNode) item;
item = map.get("id");
}
fileList.add(this.getFile(strCat, item.asText()));
}
try {
Map<String ,List<File>> zipFileMap = new HashMap<>(); //压缩文件集合
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("文件路径不正确");
}
long nTime = System.currentTimeMillis();
ZipUtils.zip2(file, zipOutputStream, zipFileMap);
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()))){
......
......@@ -123,6 +123,7 @@ public class apiSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/"+uploadpath).permitAll()
.antMatchers("/"+previewpath+"/**").permitAll()
.antMatchers("/net-disk/download/**").permitAll()
.antMatchers("/net-disk/download2/**").permitAll()
.antMatchers("/net-disk/**view/**").permitAll()
.antMatchers("/net-disk/edit/**").permitAll();
......
......@@ -13,7 +13,6 @@ import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
......@@ -23,9 +22,6 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Hashtable;
import java.util.List;
......@@ -110,6 +106,14 @@ public class DiskCoreResource
this.sendRespose(response, file);
}
@PostMapping(value = "net-disk/download2")
@ResponseStatus(HttpStatus.OK)
public void download2(@RequestBody List<JsonNode> list, HttpServletRequest request, HttpServletResponse response){
File file= diskCoreService.getFile2(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 org.springframework.util.DigestUtils;
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.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
......@@ -82,8 +85,52 @@ public class ZipUtils {
}
}
}
}
public static void zip2(File file, ZipOutputStream zipOutputStream, java.util.Map<String , List<File>> zipFileMaps) throws IOException {
String zipFileName = file.getName();
if (file.isFile()) {
String fileId = DigestUtils.md5DigestAsHex(file.getName().getBytes());
if(zipFileMaps.containsKey(fileId)){
String prefix = zipFileName.contains(".")? zipFileName.substring(0,zipFileName.lastIndexOf(".")): null;
String suffix = zipFileName.contains(".")? zipFileName.substring(zipFileName.lastIndexOf(".")) : null;
zipFileName = String.format("%s(%s)%s",prefix,zipFileMaps.get(fileId).size(),suffix);
zipFileMaps.get(fileId).add(file);
log.debug(String.format("压缩文件中已存在重复文件名,文件[%s]被重命名为[%s]" , file.getAbsoluteFile(), zipFileName));
}
else{
zipFileMaps.put(fileId, new ArrayList(){{add(file);}});
}
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
ZipEntry zipEntry = new ZipEntry(zipFileName);
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(zipFileName);
zipEntry.setTime(file.lastModified());
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.closeEntry();
} else {
for (File item : files) {
zip2(item, zipOutputStream, zipFileMaps);
}
}
}
}
public static void unzip(File zipFile, File dstFolder) throws IOException {
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册