证据下载接口,聊天功能优化
parent
92e799e9ae
commit
295e2108ee
@ -0,0 +1,39 @@
|
|||||||
|
package com.supervision.utils;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class ZipFileUtil {
|
||||||
|
|
||||||
|
public static void createZipAndDownload(HttpServletResponse response, String zipFileName, Map<String, InputStream> fileInputStreamMap) throws IOException {
|
||||||
|
response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(zipFileName, StandardCharsets.UTF_8) + ".zip");
|
||||||
|
response.setContentType("application/zip");
|
||||||
|
try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
|
||||||
|
ZipOutputStream zipOut = new ZipOutputStream(bos)) {
|
||||||
|
for (Map.Entry<String, InputStream> entry : fileInputStreamMap.entrySet()) {
|
||||||
|
InputStream inputStream = entry.getValue();
|
||||||
|
zipOut.putNextEntry(new ZipEntry(entry.getKey()));
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int length;
|
||||||
|
while ((length = inputStream.read(buffer)) >= 0) {
|
||||||
|
zipOut.write(buffer, 0, length);
|
||||||
|
}
|
||||||
|
zipOut.closeEntry();
|
||||||
|
inputStream.close();
|
||||||
|
}
|
||||||
|
zipOut.finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue