Quando eu baixo um arquivo, um .doc por exemplo, ele vem com uns caracteres doidos no inicio e no final.
O que poderia ser? O código segue abaixo para análise.
response.addHeader("Content-Type", "application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=" + documento.getNome( ));
response.addHeader("Content-Transfer-Encoding", "binary");
try
{
InputStream in = documento.getArquivo( );
int bit = 256;
int i = 0;
byte[] buffer = new byte[10 * 1024];
int nread = 0; // Number of bytes read
while( ( nread = in.read( buffer ) ) != -1 ) // Read from file
out.write( new String( buffer ), 0, nread ); // Write to Blob
in.close();
}
A Função getArquivo
public InputStream getArquivo( java.sql.Connection connection) throws SQLException
{
String strQuery = "SELECT arquivo FROM " + this.container.getTabela( ) + " WHERE id=?";
PreparedStatement statement = connection.prepareStatement( strQuery );
statement.setInt( 1, this.id );
ResultSet rs = statement.executeQuery();
rs.next( );
return rs.getBinaryStream( "arquivo" );
}