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 :)
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 :)
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("A PPLICATION /OCTET-STR EAM");
response.setHeader("Conten t-Disposit ion","atta chment; 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(dataR ead, 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("Geo Submission Servlet.do wnLoadOutp utFile() ** Output file does'nt exist " );
}
How to get over these exceptions?
File textFile = new File(outputFile);
if (textFile.exists()) {
response.setContentType("A
response.setHeader("Conten
response.setContentLength(
// 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(dataR
// close file
if (fileinputstream != null) {
fileinputstream.close();
}
// write the file to output stream
servletoutputstream.write(
// flush the output stream
servletoutputstream.flush(
}
else {
LogManager.logMessage("Geo
}
add servletoutputstream.close( );
please show button code.
please show button code.
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_FIL E_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?
<form name=convertConfirmForm" action="<%= Constants.SERVLET %>" method="post">
<span class="bodycopy"> <input type=hidden name="<%= Constants.ACTION_PARAM %>" value="<%=GeoSoftConstants
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
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
https://www.experts-exchange.com/questions/20514471/How-to-download-doc-txt-xls-pdf-through-JSP-page.html
https://www.experts-exchange.com/questions/20495514/Saving-file-from-Server-to-client-machine.html
https://www.experts-exchange.com/questions/12055559/making-a-servlet-write-a-file-to-clients-p-c.html