Pessoal, esse é um pedaço do meu servlet que faz dar dowmload de um arquivo que ele monta… so que quando abro o arquivo ele atrapalha o ^Ç~ ficando todo doido. alguem ajuda ai?
public class download extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SafoException {
Hashtable data = ContentManager.getInstance().processRequest(request);
int codArquivo = Integer.parseInt((String)data.get("codArquivo"));
ArquivoSiafi as = new ArquivoSiafi();
as.setCod(codArquivo);
as = new ArquivoSiafiDAO().selectArquivoSiafi(as);
FileInputStream fileInput = null;
try {
ReadWriteTextFile t = new ReadWriteTextFile();
String filename = as.getNomArquivoOriginal();
File f = new File(filename);
f.createNewFile();
t.setContents(f,this.getInputStreamValue(as.getArquivoIS()));
byte[] content = null;
int fileLength = (int) f.length();
fileInput = new FileInputStream(f);
BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
content = new byte[fileLength];
bufferedInput.read(content, 0, fileLength);
bufferedInput.close();
response.addHeader( "Content-Disposition",
"attachment; filename=" + f.getName());
response.setContentType( "application/download" );
ServletOutputStream outStream = response.getOutputStream();
// envia o conteúdo do arquivo para o stream de resposta
try {
outStream.write( content );
outStream.flush();
} finally {
outStream.close();
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (fileInput != null) {
fileInput.close();
}
}
}
public String getInputStreamValue(InputStream is) throws Exception {
byte[] b = null;
StringBuffer sbuf = null;
int numRead;
int j = 0;
if(is != null) {
sbuf = new StringBuffer(65535);
b = new byte[10000];
numRead = is.read(b);
while(numRead > 0) {
for(j = 0; j < numRead; ++j) {
sbuf.append((char)b[j]);
}
numRead = is.read(b);
}
}
is.close();
return sbuf.toString();
}
}