1

Heres my code to download file:

response.setContentType("text/plain"); response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment;filename=C:testing.docx"); ServletContext ctx = getServletContext(); InputStream is = ctx.getResourceAsStream("/test.docx"); int read=0; byte[] bytes = new byte[4200]; OutputStream os = response.getOutputStream(); while((read = is.read(bytes))!= -1){ os.write(bytes, 0, read); } os.flush(); os.close(); 

I was wondering if there's a way to download the file to a specific location on your desktop using the servlet code. So if I want it to download to the Document folder.

1
  • 4200 is a strange buffer size. Normally they are powers of two, like 4096, 8192, ... NB flush before close is redundant, as is setting the content-type twice. You should check is for null before using it. Commented May 13, 2015 at 22:44

1 Answer 1

1

No, there is no way to instruct the client's browser to save a file to a specific location. That is completely up to the browser (user) to decide what to do with the downloaded file.

Except if you use some other type of technology for downloading files such as ActiveX or Applet. But that's a different story...

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.