当前项目一般都是有一个独立的文件系统,前端先将文件上传至文件系统,获取到一个fileKey保存到数据库即可;再需要下载时,只需要对应的系统url+fileKey即可获取下载文件;我这里代码使用filePath代替fileKey
如何批量打包下载呢?
先获取到批量文件的list对象,对象包含两个字段fileName和filePath即可;转换为json,我这里的格式:
[
{"fileName":"我的文件1","filePath":"246542134.jpg"},
{"fileName":"我的文件2","filePath":"546542135.doc"},
{"fileName":"我的文件3","filePath":"578532136.text"},
...
]
工具类:
*fileName为文件原名,filePath为实际地址fileKey
*urlPrefix 为文件系统的url前缀,如http://xxx.com/file
package com.youdingte.saas.ewechat.biz.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 文件系统oss获取文件,打包
* zip 打包 返回byte[]
*
* @author yrl
* @date 2022/2/24 11:22
*/
public class ZipFileUtil {
/**
*json为查询文件list,我这里的格式
[{"fileName":"我的头像1","filePath":"/xxxx/2134.jpg"},
{"fileName":"我的头像2","filePath":"/xxxx/2135.jpg"},
{"fileName":"我的头像3","filePath":"/xxxx/2136.jpg"}
]
*fileName为文件原名,filePath为实际地址fileKey
*urlPrefix 为文件系统的url前缀,如http://xxx.com/file
*
*/
public static byte[] doItReport4Attachments(String json,String urlPrefix) {
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<HashMap<String,String>>> typeRef
= new TypeReference<List<HashMap<String,String>>>() {};
List<HashMap<String,String>> o = null;
try {
o = mapper.readValue(json, typeRef);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Map<String, File> srcFiles = new HashMap<>();
assert o != null;
int ix = 1;
for (HashMap<String,String> e : o) {
if (e.get("filePath") == null) {
continue;
}
File file = null;
try {
file = File.createTempFile("report4-attach-", ".tmp");
//拼接系统url+具体文件【可直接访问】如:http://xxxx.com/file/xxx/2134.jpg
copyURLToFile(new URL(urlPrefix + e.get("filePath")), file);
String key = ix + "." + e.get("fileName");
srcFiles.put(key, file);
} catch (IOException ioException) {
ioException.printStackTrace();
}
ix++;
}
Path zipPath = zip(srcFiles);
if (zipPath != null) {
try {
return Files.readAllBytes(zipPath);
} catch (IOException e) {
e.printStackTrace();
}
}
return new byte[]{};
}
public static Path zip(Map<String, File> srcFiles) {
try {
File file = File.createTempFile("multiCompressed", ".zip");
FileOutputStream fos = new FileOutputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(fos);
for (Map.Entry<String, File> srcFile : srcFiles.entrySet()) {
File fileToZip = srcFile.getValue();
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(srcFile.getKey());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
zipOut.close();
fos.close();
return file.toPath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void copyURLToFile(URL source, File destination) throws IOException {
InputStream input = source.openStream();
try {
FileOutputStream output = openOutputStream(destination);
try {
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(output);
}
} finally {
IOUtils.closeQuietly(input);
}
}
public static FileOutputStream openOutputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (!file.canWrite()) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
File parent = file.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
throw new IOException("File '" + file + "' could not be created");
}
}
return new FileOutputStream(file);
}
}
工具类返回byte[]即可,controller返回ResponseEntity<byte[]>到前端即可
我这边的测试controller如下:
@ApiOperation("批量下载")
@GetMapping("/download")
public ResponseEntity<byte[]> download(List<Long> ids) throws UnsupportedEncodingException {
//文件系统的url
String ossUrl = "http://xxxx.com/file";
byte[] bytes = null;
//ids为文件ids,查询到需要下载的文件地址
if (ids != null && ids.size() > 0) {
//SysFile测试,只有fileName和FilePath两个字段
List<SysFile> sysFiles = fileService.getByIds(ids);
if (sysFiles .size() > 0) {
//转化为json
String jsonString = JsonUtil.toJson(sysFiles);
//调用工具类
bytes = ZipFileUtil.doItReport4Attachments(jsonString, ossUrl);
}
}
if (bytes != null) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/octet-stream");
headers.add("Connection", "close");
headers.add("Accept-Ranges", "bytes");
headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode("attachments.zip", "UTF-8"));
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
} else {
return null;
}
}