/**
* 向客户端下载文件,弹出下载框.
*
* @param response
* response
* @param file
* 需要下载的文件
* @param isDel
* 下载完成后是否删除该文件
*/
public static void exportFile(HttpServletResponse response, File file,
boolean isDel) {
OutputStream out = null;
InputStream in = null;
try {
String filename = URLEncoder.encode(file.getName(), "UTF-8");
response.setContentType("application/force-download");
response.setHeader("Location", filename);
response.setHeader("Content-Disposition", "attachment;filename="
+ filename);
out = response.getOutputStream();
in = new FileInputStream(file.getPath());
byte[] buffer = new byte[1024];
int i = -1;
while ((i = in.read(buffer)) != -1)
out.write(buffer, 0, i);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
if (out != null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isDel)
deleteTempFile(file);
}