gpt4 book ai didi

java - 如何在 Java Spring Boot 中获取 zip 文件作为响应?

转载 作者:行者123 更新时间:2023-11-30 05:42:02 32 4
gpt4 key购买 nike

我正在编写代码来抓取数据库中的文件存储并将其作为响应发送给客户端。但是当我单击下载按钮时,它会发送空文本文件作为响应,但我需要 zip 文件。我哪里做错了?有人可以帮忙解决这个问题吗?

我在网络上甚至在 stackoverflow 上进行了搜索,但没有找到任何运气。

抓取所有上传文件的方法

public List<Filestore> documentCollectionBvg(Application app){
List<Filestore> candidateDocuments = new ArrayList<>();
Filestore profilePic = app.getCandidate().getProfilePic();
if(profilePic!=null){
candidateDocuments.add(profilePic);}
Filestore panDoc = app.getCandidate().getPanDocument();
if(panDoc!=null){
candidateDocuments.add(panDoc);}
Filestore aadharDoc = app.getCandidate().getAadhaarDocument();
if(aadharDoc!=null){
candidateDocuments.add(aadharDoc);
}
Filestore salarySlip = app.getCandidate().getSalarySlipDocument();
if(salarySlip!=null){
candidateDocuments.add(salarySlip);}
Filestore passportDoc = app.getCandidate().getPassportDocument();
if(passportDoc!=null){
candidateDocuments.add(passportDoc);}
return candidateDocuments;
}

Controller

@GetMapping(value = "application/get-file", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody byte[] getFile(@RequestParam("appId")String appId) throws IOException {
NDataUtils nDataUtils=new NDataUtils();
Application app=applicationService.findByEncryptedId(appId);
BgvDocumentsDownload bgv=new BgvDocumentsDownload();
List<Filestore> srcFiles=bgv.documentCollectionBvg(app);
FileOutputStream fos = new FileOutputStream("multiCompressed.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
FileInputStream fis=null;
for (Filestore srcFile : srcFiles) {
File fileToZip = new File(nDataUtils.getnDataPath()+srcFile.getFullFilePath());
fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);

byte[] bytes = new byte[1024];
int length;
while((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
zipOut.close();
fos.close();
return IOUtils.toByteArray(fis);
}

这里 NDataUtils 是一个返回路径的类。所以不要与它混淆。

点击下载链接后,会下载一个名为“download”的文件,它只是一个文本文件,完全是空的。

如果有人可以提供帮助,我会感激不尽。

最佳答案

希望这有帮助,这里的 fileLocation 是我的绝对文件路径,您可以在单独的方法中自定义您的路径。

public byte[] exportDocument(final String fileLocation, final String aEngineName, final String aJobId,
HttpServletResponse response)
throws IOException
{
String tempFileName = aEngineName + "_" + aJobId + ".zip";
response.addHeader("Content-Disposition", "attachment; filename=\"" + tempFileName + "\"");
LOG.info("Zipping of File Started");
//creating byteArray stream, make it bufferable and passing this buffer to ZipOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);

ArrayList<File> files = new ArrayList<>();

File folder = new File(fileLocation + "/out");
File[] listOfFiles = folder.listFiles();
if (listOfFiles != null)
{
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files.add(new File(listOfFiles[i].getAbsolutePath()));
}
}
}

//packing files
for (File file : files) {
//new zip entry and copying inputstream with file to zipOutputStream, then closing streams
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fileInputStream = new FileInputStream(file);

IOUtils.copy(fileInputStream, zipOutputStream);

fileInputStream.close();
zipOutputStream.closeEntry();
}

if (zipOutputStream != null)
{
zipOutputStream.finish();
zipOutputStream.flush();
IOUtils.closeSilently(zipOutputStream);
}
IOUtils.closeSilently(bufferedOutputStream);
IOUtils.closeSilently(byteArrayOutputStream);

return byteArrayOutputStream.toByteArray();
}

和RestController

@RequestMapping(value = "/export/{engineName}/{jobId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, method = RequestMethod.GET)
public byte[] exportDocument(@PathVariable("jobId") final @Nonnull String aJobId,
@PathVariable("engineName") final @Nonnull String aEngineName, HttpServletResponse aResponse)
throws Exception
{
....
byte[] output = mDataExportComponent.exportDocument(fileLocation, aEngineName, aJobId, aResponse);
return output;
.... }

关于java - 如何在 Java Spring Boot 中获取 zip 文件作为响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468163/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com