What should be the content type to download any file format in jsp?
java, jsp
Solution
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 :)
Problem
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...