I am new to Java Servlets, but would like to write files that are located on my server to a client's file systems. For example, if the client says to write to "C:\Users\Dan\Desktop", how can I write a file to that destination? I have a class that extends HttpServlet- if someone could point me in the right direction, I would appreciate it.
-
1Where does a Servlet run? Where does "the client" run? (And what issue/problem/separation does this inherently cause?) Best just to serve up a File and let "the client" (e.g. "the browser") save/open it as desired...user166390– user1663902012-05-30 23:35:09 +00:00Commented May 30, 2012 at 23:35
-
3As a web site server, you can't just decide to write a file to some location on the browser's hard drive. That would be a security hole you could drive a truck through.Greg Hewgill– Greg Hewgill2012-05-30 23:37:22 +00:00Commented May 30, 2012 at 23:37
Add a comment
|
1 Answer
The way it is done is usually to return to client a data stream that will prompt the browser to save the file. However, server can't dictate what location file will be saved to.
To do it include
Content-Type: application/octet-stream
Content-Disposition: filename=[your server recommended file name];
headers in your servlet response. Note that user can still overwrite your recommended file name in most browsers and location will be determined by browser (either default user download directory or whatever user will choose at the prompt).