This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to download file from server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to download file from server?" Watch "how to download file from server?" New topic

how to download file from server?

Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
I have a file in the server. When the client sends the file name and storing folder to the servlet. How to write the servlet to send the .java file directly to the folder?
using FileOutputStream?
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
That's not possible unless you are using a servlet-applet model with complete access to the client's machine. OR did I misunderstood your question???
Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
Yes, If using applet-servlet, how to do it? I do not want to use signed applet. Thanks
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
For applets in general this is not possible without a signed applet. Applications, of course, have no such restriction.
But you can't "push" from a server to a client, anyway. You need to have the client cooperate in some way. For example, a application client could open a socket to the server, read the response, and write it to a local file. The server cannot do this directly any more than a client can directly write to the server's local file system.
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
How would JSP implement a download page then? I am very curious...
Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
hi, Phil,
I saw one of your post with follwing code

isn't it for client side to download file?
In the applet:

It works, but I am not sure it saves into client machine or not?
I am a liitle confused here, please clarify. Thanks
Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
In the above piece of code,
the servlet
1) gets the file name parameter
2) reads the contents of the file
3) writes the contents to the browser outstream.
the applet,
1) calls the servlet passing the filename as the parameter.
2) just shows the contents (written by the servlet) in the browser window.
The applet does not save the file on the client machine, but merely passes on the content to the browser for display. However, you can always save that content using the browser "Save As .. " function.
Hope this helps.
Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
hi, Ashwin,
I tested the code above. It does save the file into the disk. It does not show the content into textarea, because I did not append it to any textarea. When I run it, it gives me a "save as" window and then save into my selected folder.
Any expl.?
It is sorta covered in the JavaRanch Style Guide.
subject: how to download file from server?
Similar Threads
How to deploy Servlet in Tomcat?
Is it possible to create a folder on the web server from a servlet?
Web hoster
Need help. Can a servlet do this?
Creating folder with Java