Expand my Community achievements bar.

Not able to create ZIP file with multiple CSV - getting the exception java.base/java.io.FileOutputStream.open0(Native Method)

Avatar

Level 2

Hi,

I am working on a report in which I need to create a ZIP file with multiple CSV files inside it. In my local AEM instance it is working fine, but when I move my code to Dev Author instance(Cloud) I am getting this error - java.base/java.io.FileOutputStream.open0(Native Method). Below is the code snippet I am using;

 

String filenamePage = "/tmp/Asset-report.csv";
outZip = new ZipOutputStream(resp.getOutputStream());

try (PrintWriter writerPage = new PrintWriter(new OutputStreamWriter(new FileOutputStream(FilenameUtils.getName(filenamePage)), UTF_8))) {
writerPage.print(csvPage); //csvPage has all the content in CSV format
} catch (IOException io) {
logger.error("\n ----ERROR -----{} ", io.getStackTrace());
}

filePage = new File(FilenameUtils.getName(filenamePage));
try (FileInputStream inPage = new FileInputStream(FilenameUtils.getName(filePage.getName()))) {
outZip.putNextEntry(new ZipEntry(filePage.getName()));
int len;
while ((len = inPage.read(buf)) > 0) {
outZip.write(buf, 0, len);
}
inPage.close();
} catch (IOException io) {
logger.error("\n ----ERROR -----{} ", io.getStackTrace());
}
filePage.delete();
outZip.closeEntry();
outZip.close();
resp.flushBuffer();

 

Since our is  Dev cloud instance, I guess when I try to create the temporary file, i don't have access to do it. Initially I give below values as temporary file name, but got the same error:

String filenamePage = "Asset-report.csv";

String filenamePage = "/content/dam/Asset-report.csv";

 

Please provide your suggestion and answers.

3 Replies

Avatar

Community Advisor

Not sure why it behaves like this, but you can try this code. This is tested for all of our environment,

private void multiFileZipDownload() throws IOException, MimeTypeException {
        ZipInputStream zipInStream = new ZipInputStream(new BufferedInputStream(null), Charset.forName("SJIS"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zipOutStream = new ZipOutputStream(baos);

        String[] filePathList = {"/content/dam/asset-1.csv", "/content/dam/asset-2.csv"};  
        for (String filePath : filePathList) {
            Resource assetResource = resourceResolver.getResource(filePath);
            Asset asset = null;
            if (assetResource != null) {
                asset = assetResource.adaptTo(Asset.class);
            }
            InputStream stream;
            if (asset != null) {
                stream = asset.getOriginal().getStream();

                byte[] bytes = IOUtils.toByteArray(stream);
                MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
                MimeType extension = allTypes.forName(asset.getMimeType());

                String documentName = asset.getName();
                ZipEntry zip = new ZipEntry(documentName.replace(extension.getExtension(), StringUtils.EMPTY) + extension.getExtension());
                zip.setSize(bytes.length);
                zipOutStream.putNextEntry(zip);
                zipOutStream.write(bytes);
            }
        }

        zipInStream.close();
        zipOutStream.close();
        response.setContentType(JcrPackage.MIME_TYPE);
        response.setHeader("Content-Disposition", "attachment; filename=\"" + "myZipFile" + "\"");
        response.getOutputStream().write(baos.toByteArray());
        response.flushBuffer();
        baos.close();
}

Avatar

Level 2

One doubt, below CSV files are already available in DAM..right?

 

 

String[] filePathList = {"/content/dam/asset-1.csv", "/content/dam/asset-2.csv"}; 

 

 

 In my case, I need to create the csv file and I am trying to that using below code snippet and I am getting the error. csvPage is the attribute, which has all the csv content.

 

Asset-report.csv is the first csv file name.

 

 

 

try (PrintWriter writerPage = new PrintWriter(new OutputStreamWriter(new FileOutputStream(FilenameUtils.getName(filenamePage)), UTF_8))) {
writerPage.print(csvPage); //csvPage has all the content in CSV format
} catch (IOException io) {
logger.error("\n ----ERROR -----{} ", io.getStackTrace());
}
filePage = new File(FilenameUtils.getName(filenamePage));

 

 

 

Avatar

Community Advisor

Hello @MaheshNarayanan5 ,

 

As per Adobe's documentation [0]:

 

The instance’s file system should not be used in AEM as a Cloud Service. The disk is ephemeral and will be disposed when instances are recycled. Limited use of the filesystem for temporary storage relating to the processing of single requests is possible, but should not be abused for huge files. This is because it may have a negative impact on the resource usage quota and run into disk limitations.

As an example where file system usage is not supported, the Publish tier should ensure that any data that needs to be persisted is shipped off to an external service for longer term storage.

 

[0]: https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/implementing/develo...

 

You might have to use AEM DAM or may be raise a support ticket to understand the possibility of writing files temporarily in File system


Aanchal Sikka