import
cn.hutool.core.io.FileUtil;
import
cn.hutool.core.io.IoUtil;
import
cn.hutool.core.util.ObjectUtil;
import
cn.hutool.core.util.StrUtil;
@Override
public
void
batchDownloadByFileIds
(List<String> fileIds, HttpServletResponse response)
{
response.setContentType(
"application/octet-stream; charset=UTF-8"
);
Map<String, InputStream> fileEntries =
new
HashMap
<String, InputStream>();
Map<String, FileInfo> fileMap =
new
HashMap
<String, FileInfo>();
try
(
InputStream
inputStream
=
filesSDKClient.downloadMulti(fileIds);
ServletOutputStream
outputStream
=
response.getOutputStream();
ZipInputStream
zis
=
new
ZipInputStream
(inputStream);
File
zipFile
=
File.createTempFile(
"package"
,
".zip"
);
for
(String fileId : fileIds) {
FileInfo
fileInfo
=
filesSDKClient.getFileInfo(fileId);
if
(ObjectUtil.isNotEmpty(fileInfo)) {
fileMap.put(fileInfo.getId(), fileInfo);
ZipEntry
zipEntry
=
null
;
while
((zipEntry = zis.getNextEntry()) !=
null
) {
fileEntries.put(zipEntry.getName(), convertZipInputStreamToInputStream(zis));
zipFiles(fileEntries, fileMap, zipFile);
FileInputStream
fis
=
new
FileInputStream
(zipFile);
response.setHeader(
"Content-disposition"
,
"attachment;filename*=utf-8''"
+
"package.zip"
);
IoUtil.copy(fis, outputStream);
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(FilesSDKException e) {
e.printStackTrace();
private
InputStream
convertZipInputStreamToInputStream
(
final
ZipInputStream in)
throws
IOException {
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
IoUtil.copy(in, out);
InputStream
is
=
new
ByteArrayInputStream
(out.toByteArray());
return
is;
private
void
zipFiles
(Map<String, InputStream> fileEntries, Map<String, FileInfo> fileMap, File zipFile)
{
try
(
ZipOutputStream
zos
=
new
ZipOutputStream
(
new
FileOutputStream
(zipFile));) {
for
(Map.Entry<String, InputStream> fileEntry : fileEntries.entrySet()) {
String
fileKey
=
fileEntry.getKey();
FileInfo
fileInfo
=
fileMap.get(fileKey);
if
(ObjectUtil.isNotEmpty(fileInfo)) {
zos.putNextEntry(
new
ZipEntry
(fileInfo.getFileName()));
IoUtil.copy(fileEntry.getValue(), zos);
zos.closeEntry();
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException ioException) {
ioException.printStackTrace();
复制代码