0

This may be a dumb / ignorant / basic question, but I am brand new to Tomcat, and a little confused.

I'd like to have a user visit a link and have my servlet automatically start a download. I know that I can return text or HTML by using:

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        //etc...
    }

But how do I return a file that is on my server, and have the browser start a download?

sh7411usa
  • 196
  • 1
  • 13
  • 1
    Does this answer your question? [Implementing a simple file download servlet](https://stackoverflow.com/questions/1442893/implementing-a-simple-file-download-servlet) – takendarkk Jul 27 '20 at 16:44

1 Answers1

-1

You need to set the content type. Also see this answer: Do I need Content-Type: application/octet-stream for file download?

For instance: resp.setContentType("application/octet-stream");

aeberhart
  • 744
  • 1
  • 4
  • 15