Я пишу код, который захватывает хранилище файлов в базе данных и отправляет его в ответ клиенту.Но когда я нажимаю кнопку загрузки, он отправляет пустой текстовый файл в качестве ответа, но мне нужен 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 - это класс, который возвращает путь для where.Так что не запутайтесь с ним.
После того, как я нажму на ссылку для скачивания, файл с именем "download" будет загружен, который будет просто текстовым файлом и будет пустым.
Если кто-то может помочь, это будет мило с вашей стороны.