Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

downloaded file not readable

 
su na
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all
I have a requirement in my app to download and upload.
I have done for uploading without any error.
For downloading there is some problem,file is downloading but its not readable.Suppose the download fiile is 50mb and after downloading when you check the size then also it says 50mb but its not readable.
Please tell me where do i have make changes.

<%@ 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());
}
%>
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you download a file, you shouldn't use a JSP to do that. The reason is that the whitespace from the JSP will be sent to the response as well as the data you send in your Java code. This will corrupt your download and make it unusable. So, first of all you should change your code to use a servlet, not a JSP.

And second of all, the file types you are downloading are binary files, not text files. So don't use a BufferedReader to get the data from the file, use an InputStream of some kind. BufferedInputStream should work nicely.
 
su na
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
I will try the way you said
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!