0

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.

2
  • 1
    Where 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... Commented May 30, 2012 at 23:35
  • 3
    As 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. Commented May 30, 2012 at 23:37

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.