Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:38:59+00:00 2026-06-06T15:38:59+00:00

I have the following block of code in my bean class – HttpServletResponse response

  • 0

I have the following block of code in my bean class –

HttpServletResponse response = (HttpServletResponse) getFacesContext().getExternalContext().getResponse();
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());

From my bean I call my service layer, which in turn calls the database layer, retrieves data and writes the files generated in a ZIP file in the service layer. I have an issue where I do not have a clue what to do if there are no files generated.

When I instantiate ZipOutputStream object in my bean, it creates a ZIP file and opens up a “Opening Extracts.zip” window, and when I click on OK, I get an error because there are no files in the ZIP folder.

Can somoone please tell me if there is any way I can move the zos instantiation to the service layer without having to pass the response object?

Thanks,
Raaz

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:39:01+00:00Added an answer on June 6, 2026 at 3:39 pm

    I found I can do the following to fix my issue –

    In my bean class, I wrote the following code –

    HttpServletResponse response = (HttpServletResponse) getFacesContext().getExternalContext().getResponse();
    ByteArrayOutputStream outputStream = null;
    
    try {
        outputStream = //Call service to generate the extract, return outputStream ; 
        if(outputStream != null){
        response.setContentType(T3Constants.MIME_ZIP);
        response.setHeader(T3Constants.CONTENT_DISPOSITION, ATTACH_FILE_NAME);
        response.getOutputStream().write(outputStream.toByteArray());
        response.flushBuffer();
        FacesContext.getCurrentInstance().responseComplete();
        outputStream.close();
        } else {
        formatFacesMessage(FacesMessage.SEVERITY_WARN, "No data for selected filter", "No data for selected filter");
        }
    }
    

    In my service, I wrote the following code –

    ByteArrayOutputStream outputStream = null;
    if(CollectionUtils.isNotEmpty(list returned from DB)) {
        outputStream = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(outputStream);
        //Add files to ZIP
        outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        zos.putNextEntry(new ZipEntry(xlsName.toString()));
        zos.write(outputStream.toByteArray());
        zos.flush();
        zos.close();
    return outputStream;
    }
    

    Thanks,
    Raaz

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following block of code: class Student{ int age; //instance variable String
Hi I have following code block public class Driver { static String x =
I have the following block of HTML code more than once <div id=page_1 class=page>
I have the following code block: - using System.ComponentModel.DataAnnotations; public class NewsItem { [RegularExpression(System.Configuration.ConfigurationSettings.AppSettings[UrlRegEx],
So I have the following code block: var sw = new Stopwatch(); sw.Start(); while
I have the following block of code. Can I have a line that removes
i have the following block of code: $(#contact_container form, #contact_details form).live( submit, function(event) {
I have the following block of code which works fine; var boughtItemsToday = (from
given i have the following block of code (function(){ var mb = { abc:function(){
I have the following code block that fails: this.redisClient.hmset('user:' + userObj.getUserId(), { 'userId' :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.