文件下载到页面
public static void downloadToPage(HttpServletResponse response, File file) {
OutputStream os = null;
try {
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(filename.getBytes("utf-8"), "iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);
os.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
os.close();
}catch (IOException e){
e.printStackTrace();
}
}
}