Link to home
Start Free TrialLog in
Avatar of rsvln
rsvln

asked on

Download a file on clicking a button

Hello Forum Members,
      I want to download a file (any type of file) from the server irrespective of the location (on the server) by clicking a button.
The code can be either in the Servlet or JSP.

Any thoughts/code that accomplishes task this will be highly appreciated.

Thanks :)
Avatar of rrz
rrz
Flag of United States of America image

Avatar of rsvln
rsvln

ASKER

I have the following code in place but it throws an IllegalStateException and if I have some other button other than the Download it throws a NullPointer Exception.
How to get over these exceptions?


File textFile = new File(outputFile);
          if (textFile.exists()) {

              response.setContentType("APPLICATION/OCTET-STREAM");
                    response.setHeader("Content-Disposition","attachment; filename=\""+ textFile.getName() + "\"");
                 response.setContentLength((int) textFile.length());
                  // set output stream to the response's output stream
                   ServletOutputStream servletoutputstream = response.getOutputStream();
                   // now read file
                   byte[] dataRead = new byte[(int)textFile.length()];
                   FileInputStream fileinputstream = new FileInputStream(textFile);
                   // read in byte data from file and store in byte array
                  fileinputstream.read(dataRead, 0, (int)textFile.length());
                   // close file
                   if (fileinputstream != null) {
                       fileinputstream.close();
                   }
                      // write the file to output stream
                   servletoutputstream.write(dataRead);
                      // flush the output stream
                   servletoutputstream.flush();
       }
       else {
                       LogManager.logMessage("GeoSubmissionServlet.downLoadOutputFile() ** Output file does'nt exist " );

       }
add    servletoutputstream.close();
please show button code.
Avatar of rsvln

ASKER

Button code:

<form name=convertConfirmForm" action="<%= Constants.SERVLET %>" method="post">
            <span class="bodycopy">     <input type=hidden name="<%= Constants.ACTION_PARAM %>" value="<%=GeoSoftConstants.DOWNLOAD_OUTPUT_FILE_ACTION%>">
     <input type="submit"  name="downloadOutputFile" value="Download Output File">
            </span>
          </form>

IS there a way I can use this code to download multiple files?







ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rsvln:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-- Points for rrz@871311

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer