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.