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

  • Home
  • SEARCH
  • 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 6556535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:57:25+00:00 2026-05-25T12:57:25+00:00

In my Java Based Web application, I am trying to write some files in

  • 0

In my Java Based Web application, I am trying to write some files in a ZIP file and I want to prompt the user to download/cancel/save. The time when the download dialog box opens and if I click on cancel, after then if I try to access any links in my application then the dialog box opens again. Here’s my code snippet.

private void sendResponse(byte[] buf, File tempFile) throws IOException {
    long length = tempFile.length();
    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    String disposition = "attachment; fileName=search_download.zip";
    ServletOutputStream servletOutputStream = null;
    InputStream in = null; 
    try {
        if (buf != null) {
            in = new BufferedInputStream(new FileInputStream(tempFile));
            servletOutputStream = response.getOutputStream();
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", disposition);
            while ((in != null) && ((length = in.read(buf)) != -1)) {
                servletOutputStream.write(buf, 0, (int) length);
            }
        }
    } finally {
        if (servletOutputStream != null) {
            servletOutputStream.close();
        }
        if (in != null) {
            in.close();
        }
        if (tempFile != null) {
            tempFile.delete();
        }
    }
    context.responseComplete();
}

Also once I click on save/Open, its working as expected. I hope the problem in clearing the response object. Please help me in providing some solutions.

EDIT
downloadSelected Method

      public void downloadSelected() throws IOException {

    List<NodeRef> list = init();
    StringBuffer errors = new StringBuffer("");
    ZipOutputStream out = null;

    File tempFile = null;
    byte[] buf = null;
    try {
        if (list != null && list.size() > 0) {
            tempFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX_ZIP);
            out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tempFile)));
            buf = writeIntoZip(list,out);
            sendResponse(buf,tempFile);
        } else {
            errors.append("No Items Selected for Download");
            this.errorMessage = errors.toString();
        }
    } 
    catch(IOException e) {
        System.out.println("Cancelled");
    }       
}

Write into Zip method:

private byte[] writeIntoZip(List<NodeRef> list,ZipOutputStream out) throws IOException {
    String downloadUrl = "";
    InputStream bis = null;
    Node node = null;
    String nodeName = "";
    byte[] buf = null;
    Map<String,Integer> contents = new HashMap<String, Integer>();
    ContentReader reader = null;
    for (NodeRef nodeRef : list) {
        try {
            node = new Node(nodeRef);
            nodeName = node.getName();
            reader = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getContentService().getReader(nodeRef, ContentModel.PROP_CONTENT);
            bis = new BufferedInputStream(reader.getContentInputStream());
            if (bis != null) {
                contents = setFiles(contents,nodeName);
                nodeName = getUniqueFileName(contents, nodeName);
                buf = new byte[4 * 1024];
                buf = writeOutputStream(bis).toByteArray();
                out.putNextEntry(new ZipEntry(nodeName));
                out.write(buf);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if(out != null) {
                out.close();
            }
        }
    }
    if(out != null) {
        out.close();
    }
    return buf;
}

Thanks,
Jeya

  • 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-05-25T12:57:26+00:00Added an answer on May 25, 2026 at 12:57 pm

    I’m admittedly not sure of the root cause of this problem. This behaviour is not explainable based on the code posted as far. An SSCCE would definitely help more. But I spot several potential causes of this problem. Perhaps fixing one or all of them will fix the concrete problem.

    1. You’ve assigned JSF’s FacesContext as a property of the bean. This is bad and definitely if it’s a bean with a broader scope than the request scope. It should always be obtained inside the local method scope by FacesContext#getCurrentInstance(). It’s returns namely a thread local variable and should never be shared among other requests. Perhaps you’ve put the bean in the session scope and a dangling response object of the previous request with the headers already set will be reused.

    2. You are not catching the IOException on close() methods. If the client cancels the download, then the servletOutputStream.close() will throw an IOException indicating that the client has aborted the response. In your case, the in won’t be closed anymore and the tempFile won’t be deleted anymore and the JSF response won’t be completed anymore. You should also catch the close() and log/ignore the exception so that you can ensure that the finally finishes its job. Perhaps the presence of tempFile has consequences for your future POST actions.

    3. You are using <h:commandLink> instead of <h:outputLink> or <h:link> or plain <a> for page-to-page navigation. This uses POST instead of GET which is bad for user experience and SEO. You should use GET links instead of POST links. See also When should I use h:outputLink instead of h:commandLink?

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

Sidebar

Related Questions

We have a java based enterprise web application. User enter / edit data using
I'm trying to create a web user interface for a Java application. The user
I am trying to read a properties file in my java web application. I
Let's share Java based web application architectures! There are lots of different architectures for
I have a Java based web-application using Java Server Faces and Facelets. I am
We have a Java EE-based web application running on a Glassfish app server cluster.
Hi I have a page in my java/jsp based web application which shows list
Let's say I have a running Java-based web application with 0 or more valid
I'm currently developing a Java-based web application on my MacBook. Most of my testing
Context: The Cloud We have a java-based web application that we normally host on

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.