1

I have JBoss running as application server and somewhere on my HD there is a PDF file, that gets created when the user clicks on a specific action. Let's say the file is here: C:/PDF/doonot/10.07.2012/doonot.pdf. How can I offer this file as download? I already did it for a CSV file, but I don't know how to do it with PDF.

Any help is much appreciated.

nimrod
  • 5,595
  • 29
  • 85
  • 149

3 Answers3

1

as i wrote on Is there a common way to download all types of files in jsp?

you can use something like this:

public HttpServletResponse getFile (HttpServletRequest request ,HttpServletResponse httpServletResponse, .......){
          HttpServletResponse response = httpServletResponse;
          InputStream in =/*HERE YOU READ YOUR FILE AS BinaryStream*/

          String filename = "";
          String agent = request.getHeader("USER-AGENT");
          if (agent != null && agent.indexOf("MSIE") != -1)
          {
            filename = URLEncoder.encode(/*THIS IS THE FILENAME SHOWN TO THE USER*/, "UTF8");
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition","attachment;filename=" + filename);
          }
          else if ( agent != null && agent.indexOf("Mozilla") != -1)
          {
            response.setCharacterEncoding("UTF-8");
            filename = MimeUtility.encodeText(/*THIS IS THE FILENAME SHOWN TO THE USER*/, "UTF8", "B");
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
          }


          BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
          byte by[] = new byte[32768];
          int index = in.read(by, 0, 32768);
          while (index != -1) {
              out.write(by, 0, index);
              index = in.read(by, 0, 32768);
          }
          out.flush();

          return response;
}

UPDATE:

Dont forget that you can use the InputStream as this:

// read local file into InputStream
InputStream inputStream = new FileInputStream("c:\\SOMEFILE.xml");

or you can use it even like this

//read from database
Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream();
Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
0

You can simply write a servlet wich read the pdf and write it to the response output stream.

Exemple here : http://www.java-forums.org/blogs/servlet/668-how-write-servlet-sends-file-user-download.html

Anthony
  • 429
  • 1
  • 4
  • 10
  • Good input, gonna try this one! Thx – nimrod Jul 11 '12 at 08:51
  • "There was an error opening this document. The file is damaged and could not be repaired"... – nimrod Jul 11 '12 at 11:15
  • you probably add unwanted data at the beginning or at the end of the file, or not flush, or whatever. Try to download a simple text file with your servlet to see what's going on. – Anthony Jul 12 '12 at 07:38
0

Yes Gustav is right. Java doesn't discriminate amongst file types. A file is a file, if you did it for csv, it should also work for pdf.

greenkode
  • 4,001
  • 1
  • 26
  • 29