/**
* 下载文件
* @param file(文件)
* @param displayName(下载时显示的文件名)
* @param response
*/
public static void download(String file, String displayName,
HttpServletResponse response) {
try {
displayName = URLEncoder.encode(displayName, "UTF-8");
} catch (UnsupportedEncodingException e1) {
log.exception(e1);
}
response.setContentType("application/x-download");// 设置为下载application/x-download
response.addHeader("Content-Disposition", "attachment;filename="
+ displayName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(
FileTool.PATH_REAL_ROOT + file));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
log.exception(e);
} finally {
if (bis != null)
try {
bis.close();
} catch (IOException e) {
log.exception(e);
}
bis = null;
if (bos != null)
try {
bos.close();
} catch (IOException e) {
log.exception(e);
}
bos = null;
}
}