I am trying to download files using jsp but then downloaded files are not readable.Suppose if i download pdf files then it can not opened,if i download jpg files then it can not opened and similarily for the video files too.Please point out the mistakes where i have made    <%@ page import = java.sql.*%><%@ page import = java.util.* %><%@ page import= java.io.* %><%try{String txtFileNameVariable=movie.mp4;String locationVariable=../webapps/y/;String PathVariable=;//txtFileNameVariable = request.getParameter(fileVariable);//locationVariable = request.getParameter(locationVariable);PathVariable = locationVariable+txtFileNameVariable;BufferedReader bufferedReader = null;try{bufferedReader = new BufferedReader(new FileReader(PathVariable));}catch(FileNotFoundException fnfe){fnfe.printStackTrace();}File f=new File(locationVariable, txtFileNameVariable);String fileType = txtFileNameVariable.substring(txtFileNameVariable.indexOf(.)+1,txtFileNameVariable.length());if (fileType.trim().equalsIgnoreCase(txt)) {response.setContentType( text/plain );} else if (fileType.trim().equalsIgnoreCase(doc)) {response.setContentType( application/msword );} else if (fileType.trim().equalsIgnoreCase(xls)) {response.setContentType( application/vnd.ms-excel );} else if (fileType.trim().equalsIgnoreCase(pdf)) {response.setContentType( application/pdf );} else {response.setContentType( application/octet-stream );}String original_filename = txtFileNameVariable;response.setHeader( Content-Disposition, attachment; filename=\ + original_filename + \ );try{int anInt=0;while((anInt=bufferedReader.read())!=-1)out.write(anInt);}catch(IOException ioe){ioe.printStackTrace();}}catch(Exception e){out.println(This is the Error  +e.getMessage());}%>
file downloading using jsp but not readable
java;servlets;file;jsp
Some observations:Don't use FileReader, that's meant to be used for char streams, where undesireable transformations could happen due to default charsets and the like. Binary streams are the correct choice for your case.Your first try-catch sentence doesn't handle the error apropriately, you only print the stacktrace, but your code will probably throw a NullPointerException at the first reference to bufferedReader.Maybe this is a simplified version of your JSP, but just in case: have you noticed that you are not validating the user input? (I guess you get the filename from a request parameter, as you have commented a line where the filename is taken from the request) what if, say, you receive something like ../../../etc/passwd? this is a security issue.Scriptlets are deprecated, consider using JSTL tags or custom ones.You are opening a stream (bufferedReader) but you never close it.Anyway, this is a version of your JSP using InputStream instead of Reader, I haven't fixed the other issues I mentioned (that is your task):<%@ page import = java.sql.*%><%@ page import = java.util.* %><%@ page import= java.io.* %><%try{    String txtFileNameVariable=movie.mp4;    String locationVariable=../webapps/y/;    String PathVariable=;    //txtFileNameVariable = request.getParameter(fileVariable);    //locationVariable = request.getParameter(locationVariable);    PathVariable = locationVariable+txtFileNameVariable;    BufferedInputStream bufferedInputStream = null;    try{        bufferedInputStream = new BufferedInputStream(new FileinputStream(PathVariable));    }    catch(FileNotFoundException fnfe){        fnfe.printStackTrace();    }    File f=new File(locationVariable, txtFileNameVariable);    String fileType = txtFileNameVariable.substring(txtFileNameVariable.indexOf(.)+1,txtFileNameVariable.length());    if (fileType.trim().equalsIgnoreCase(txt)) {        response.setContentType( text/plain );    } else if (fileType.trim().equalsIgnoreCase(doc)) {        response.setContentType( application/msword );    } else if (fileType.trim().equalsIgnoreCase(xls)) {        response.setContentType( application/vnd.ms-excel );    } else if (fileType.trim().equalsIgnoreCase(pdf)) {        response.setContentType( application/pdf );    } else {        response.setContentType( application/octet-stream );    }    String original_filename = txtFileNameVariable;    response.setHeader( Content-Disposition, attachment; filename=\ + original_filename + \ );    try{        int anInt=0;        OutputStream output = response.getOutputStream();        while((anInt=bufferedInputStream.read())!=-1)            output.write(anInt);        output.flush();    } catch(IOException ioe) {        ioe.printStackTrace();    }} catch(Exception e) {    out.println(This is the Error  +e.getMessage());}%>