title: Can I force the browser to download the PDF file instead of opening it?
body: <p>So this is the code I have:</p>
  
  <pre><code>public class PdfDownloaderServlet extends HttpServlet {
  
      @Override
      protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
              throws ServletException, IOException {
          httpServletResponse.setContentType("application/pdf");
          ServletContext servletContext = httpServletRequest.getServletContext();
          InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/pdfs/x.pdf");
          int read;
          byte[] bytes = new byte[1024];
          OutputStream os = httpServletResponse.getOutputStream();
          while ((read = inputStream.read(bytes)) != -1) {
              os.write(bytes, 0, read);
          }
          os.flush();
          os.close();
      }
  }
  </code></pre>
  
  <p>and it works just fine. </p>
  
  <p>However when I click the link that invokes this method, the browser will open the file, but I want the browser to directly download the file. How can I achieve this?</p>
  
  <p>Thanks.</p>