V_Ram
asked on
How do i download a file to my Client Machine
Hello All,
I am currently working on a Java project involving Servlet, Jsp and Oracle. Currently i am using Tomcat as my servlet container.
In the project i have a requirement to download a export file to the Client's machine. Does any one have idea about how to perform the same.
This is very urgent.
V. Ramkumar.
I am currently working on a Java project involving Servlet, Jsp and Oracle. Currently i am using Tomcat as my servlet container.
In the project i have a requirement to download a export file to the Client's machine. Does any one have idea about how to perform the same.
This is very urgent.
V. Ramkumar.
ASKER
Hello Ovi,
You talk about client side scripting, but i do not need any client side scripting. Is there any way that i could do with servlets.
Thanks for your efforts.
V. Ram.
You talk about client side scripting, but i do not need any client side scripting. Is there any way that i could do with servlets.
Thanks for your efforts.
V. Ram.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it, Thanks.By the way how do i give a default file name to the file that's going to be downloaded.
V. Ramkumar
V. Ramkumar
What do you understand by a default name ?
ASKER
The name given to the file that's going to be downloaded.
set the content type of the response to whatever mimetype has the file if is normaly downloadable or force to application mimetype, read the file and write'it on the response.
client side :
open a connection to your url, get the inputstream, read from it and write the readed content as a new file.
URL url = new URL("your url here");
InputStream is = url.openStream();
FileOutputStream fos = new FileOutputStream("a file");
byte[] buffer = new byte[4096];
while(is.read(buffer, 0, buffer.length) != -1) {
fos.write(buffer);
fos.flush();
}
...add the required try&catch statements, and of course close methods for streams.