0

I want to download office files,pdf files,image files,zip files,dll files,exe files based on the user selection.So, I want to download these file types from jsp page.

This is jsp code snippt:

<% 
String filename = "Sample1.docx"; 
String filepath = "e:\\temp\\"; 
response.setContentType("APPLICATION/OCTET-STREAM"); 
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); 

java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename);

int i; 
while ((i=fileInputStream.read()) != -1) {
    out.write(i); 
} 
fileInputStream.close();
%>

But it raising some error when downloading office files, images files.When i open the downloaded files it says "The File may be Corrupted".

Is there any common way to download all types of files in jsp?

Elazar
  • 20,415
  • 4
  • 46
  • 67
Saravanan
  • 11,372
  • 43
  • 143
  • 213
  • Related: http://stackoverflow.com/questions/4543936/load-the-image-from-outside-of-webcontext-in-jsf/4543951#4543951 – BalusC May 22 '12 at 17:25

3 Answers3

3

Your problem is that the out variable in a JSP is a JspWriter, which is a character stream and so your binary files get altered. You would be much better off using a servlet directly for this particular purpose.

mprivat
  • 21,582
  • 4
  • 54
  • 64
1

ok, there are some issues when downloading files from different browsers. My example takes care the handling needed in MSIE and Mozilla type of browsers

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;
}

check this out

UPDATED

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
1

The problem is with JSP's "Out.write", which is not capable of writing byte stream...

Replaced jsp file with servlet...

The code snippet is:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        String filename = (String) request.getAttribute("fileName");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment;filename="+filename);

        File file = new File(filename);
        FileInputStream fileIn = new FileInputStream(file);
        ServletOutputStream out = response.getOutputStream();

        byte[] outputByte = new byte[(int)file.length()];
        //copy binary contect to output stream
        while(fileIn.read(outputByte, 0, (int)file.length()) != -1)
        {
        out.write(outputByte, 0, (int)file.length());
        }
     }

Now U can download all types of files....

loknath
  • 738
  • 2
  • 8
  • 20