Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be the content type to download any file format in jsp?

Tags:

java

jsp

I want to make a provision to download all file types...Is there any way to download any file format in jsp...

My code snippet:

    String filename = (String) request.getAttribute("fileName");        
    response.setContentType("APPLICATION/OCTET-STREAM");
    String disHeader = "Attachment";
    response.setHeader("Content-Disposition", disHeader);

    // transfer the file byte-by-byte to the response object
    File fileToDownload = new File(filename);
    response.setContentLength((int) fileToDownload.length());
    FileInputStream fileInputStream = new FileInputStream(fileToDownload);
    int i = 0;
    while ((i = fileInputStream.read()) != -1) {
        out.write(i);
    }
    fileInputStream.close();

If I specify setContentType as APPLICATION/OCTET-STREAM, pdf, text, doc files are getting downloaded.... But the problem is with image files...

What is problem with image files? I want to download all image file types...

I searched similar questions but could not find proper answer... Thanks...

like image 616
loknath Avatar asked Dec 03 '12 06:12

loknath


People also ask

How do I download a CSV file from a website in Java?

setContentType("application/octet-stream"); response. setHeader("Content-Disposition", "attachment;filename=downloadfilename. csv"); we can also specified a download file name in attachment;filename=, above example export a csv file name “downloadfilename.

What is a JSP file?

Jakarta Server Pages (JSP; formerly JavaServer Pages) is a collection of technologies that helps software developers create dynamically generated web pages based on HTML, XML, SOAP, or other document types. Released in 1999 by Sun Microsystems, JSP is similar to PHP and ASP, but uses the Java programming language.


3 Answers

Finally I somehow managed to do this... The problem is with JSP's "Out.write", which is not capable of writing byte stream...

I 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 I can download all types of files....

Thanks for the responces :)

like image 73
loknath Avatar answered Nov 14 '22 21:11

loknath


Check the following link ,

JSP download - application/octet-stream

Might help you to resolve the issue.

like image 32
krish Avatar answered Nov 14 '22 21:11

krish


for images you should use setContentType(image/jpg).you can checkout this link for mime types

http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm

like image 28
arjun3037 Avatar answered Nov 14 '22 23:11

arjun3037