Hallo,
ich möchte gerne ein ZIP File erstellen und das in einem Stream von der Web GUI zurückgeben:
in meiner Controller - Klasse:
Ausgabe in GUI (Primefaces):
Es gibt mir auch eine Datei zurück.
Allerdings enthält die ZIP -Datei keine Dateien, sondern sieht so aus:
Kann hier jemand weiterhelfen?
ich möchte gerne ein ZIP File erstellen und das in einem Stream von der Web GUI zurückgeben:
Java:
/**
* Generiert ein File, das heruntergeladen werden kann
*
* @throws IOException
* @throws PicPictureNotFoundException
*/
public ByteArrayInputStream createZipFileForWholePicAlbum(PicAlbum picAlbum)
throws IOException, PicPictureNotFoundException {
List<PicPicture> picPictureList = picPictureService.findAllPicPictureFromPicAlbum(picAlbum);
if (picPictureList == null || picPictureList.isEmpty())
throw new PicPictureNotFoundException();
// Destination of the ZIP file (an in-memory byte array)
ByteArrayOutputStream boas = new ByteArrayOutputStream();
/*
* Write the ZIP file. This creates a single entry named "file.txt" with
* "Hello, World!" as its contents.
*/
try (ZipOutputStream zos = new ZipOutputStream(boas)) {
for (PicPicture picPicture : picPictureList) {
File file = new File(picPicture.getStorageAttachment().getFullPath());
BufferedInputStream bufferedInputStream = null;
byte data[] = new byte[1024];
FileInputStream fileInputStream = new FileInputStream(file.getAbsolutePath());
bufferedInputStream = new BufferedInputStream(fileInputStream, 1024);
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
int count;
while ((count = bufferedInputStream.read(data, 0, 1024)) != -1) {
zos.write(data, 0, count);
}
bufferedInputStream.close();
fileInputStream.close();
zos.closeEntry();
}
}
// Create an InputStream to read the raw bytes of the ZIP file
ByteArrayInputStream bois = new ByteArrayInputStream(boas.toByteArray());
return bois;
}
in meiner Controller - Klasse:
Java:
public void createZipFileForWholePicAlbum(PicAlbum picAlbum) throws IOException, PicPictureNotFoundException {
private StreamedContent streamFile:
try {
ByteArrayInputStream is = picGalleryDownloadProcessService.createZipFileForWholePicAlbum(picAlbum);
streamFile = DefaultStreamedContent.builder()
.contentType("application/zip")
.name("MeineBilder.zip")
.stream(() -> is).build();
}
catch (Exception e) {
LOGGER.error(ExceptionUtils.getFullStackTrace(e));
ErrorMessage.showErrorMessage();
}
}
Ausgabe in GUI (Primefaces):
Java:
<p:commandButton value="Alle Bilder herunterladen"
actionListener="#{actionPicGalleryDownloadController.createZipFileForWholePicAlbum(picAlbumForPortalShowController.picAlbum)}"
icon="fa fa-download" styleClass="GreenButton">
<p:fileDownload value="#{actionPicGalleryDownloadController.streamFile}" />
</p:commandButton>
Es gibt mir auch eine Datei zurück.
Allerdings enthält die ZIP -Datei keine Dateien, sondern sieht so aus:
Kann hier jemand weiterhelfen?