• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

downloaded file not readable

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
%>
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
I will try the way you said
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic