0

I am trying to download a file to the client machine, the file exists in my servlet WEB-INF folder

So, in my doGet() I have something like this,

String path = "/u/my/workspace/FirstServlet/WebContent/WEB-INF/04:04:2014/logs.txt";
String result = "<a href='"+path+"' target='_blank'>Download result</a>";
request.setAttribute("ViewResult", result);

in my jsp I have,

<div>${ViewResult}</div>

but this is not downloading the file but is returning a HTTP 404 requested resource not found even though the file is there.

So based on this answer I've tried

response.setHeader("Content-Type", "text");
response.setHeader("Content-Disposition", "attachment;filename="+path);

though it has downloaded a file, the contents of the file are same as my jsp page source code.

How to do this right?

Community
  • 1
  • 1
Code
  • 785
  • 3
  • 8
  • 19
  • 1
    Everything within `WEB-INF` is not accessible externally. A path on your server is not a link to a webpage. – Sotirios Delimanolis Apr 04 '14 at 03:42
  • Okay so how do I do this? what should be the work around? – Code Apr 04 '14 at 03:43
  • 1
    you could do something more along the lines of this: http://balusc.blogspot.ca/2007/07/fileservlet.html – Will Newton Apr 04 '14 at 03:44
  • you write to the response's output stream. You can do a search in that last page for 'response.getOutputStream()' you will see a try/finally where the bytes from the file are written to the response's output stream – Will Newton Apr 04 '14 at 03:47
  • 1
    @Code you're on the right track with the code you already have using the response.setHeader methods. Keep going. you can do it – Will Newton Apr 04 '14 at 03:48
  • possible duplicate of [Download file from servlet](http://stackoverflow.com/questions/22852288/download-file-from-servlet) – jmail Apr 04 '14 at 05:34

0 Answers0