Problem in Reading the File Content after downloading
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Saturday, October 9, 2004 06:13 AM

I have downloaded a file, but after opening the file it contains only question marks as its content. But this same piece of code works fine and file content can be read while downloading files like Text Files, but it doesnt work for other files like word documents, image files etc. Any help is greatly appreciated. The Sample Code is given below : Here "file" is the file object which needs to be downloaded. ******************************************************* **** public static String readFile(String sAbsPath){ File file = new File(sAbsPath); String sFileContent=null; BufferedReader reader = null; try{    More>>

I have downloaded a file, but after opening the file it contains only question marks as its content. But this same piece of code works fine and file content can be read while downloading files like Text Files, but it doesnt work for other files like word documents, image files etc. Any help is greatly appreciated.


The Sample Code is given below :
Here "file" is the file object which needs to be downloaded.


******************************************************* ****


public static String readFile(String sAbsPath){


File file = new File(sAbsPath);


String sFileContent=null;

BufferedReader reader = null;



try{

reader = new BufferedReader(new
FileReader(file));

}catch(FileNotFoundException fnEx){

// Handle errors

return null;

}//end catch


//read the file

StringBuffer sbFile = new StringBuffer();

int iChar = 0;



try{

while((iChar = reader.read()) > 0){

sbFile.append((char)iChar);

}

//close the reader

reader.close();

}catch(IOException ioEx){

//TODO:need to add error message here

return null;

}//end catch



sFileContent= sbFile.toString();

return sFileContent

}

*************************************************************


This is the code that i am using for downloading


response.setContentType("application/x-download");

response.setHeader("Content-Disposition", "attachment;
filename="" + file.getName() + """);

response.setContentLength((int) file.length());

   <<Less

Re: Problem in Reading the File Content after downloading

Posted By:   Stephen_Ostermiller  
Posted On:   Sunday, October 10, 2004 05:21 PM

You should be treating files as byte streams, not character streams. The problem is is that you open a stream of bytes and with a FileReader. When you use a FileReader, you are assuming that the bytes represent characters and Java does the byte to character translation for you. That is why it works for text files, but not for other files.


What you really want to do is this:


response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename="" + file.getName() + """);
response.setContentLength((int) file.length());
OutputStream out = response.getOutputStream()
InputStream in = new FileInputStream(file.getName());
byte[] buffer = new byte[1024];
int read;
while((read=in.read(buffer))!=-1){
out.write(buffer,0,read);
}


Don't use Reader, don't use Writers, don't use Strings, don't use StringBuffers. All of those assume that the file contains character data.


Do use InputStreams, OutputStreams, and byte arrays.

About | Sitemap | Contact