java文件下载

/**
* zip 导出
* @param response
* @param zipName
* @throws Exception
*/
private void outZip(HttpServletResponse response, String zipName) throws Exception {
// tempFilePath 为服务器上文件保存路径
String zipPathName = tempFilePath + File.separator + zipName;
BufferedOutputStream out = null;
InputStream in = null;
try {
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipName, "UTF-8"));
out = new BufferedOutputStream(response.getOutputStream());// 获取输出流
in = new BufferedInputStream(new FileInputStream(new File(zipPathName)));// 将文件读取
byte[] b = new byte[in.available()];
in.read(b);
out.write(b);
out.flush();
} catch (Exception e) {
throw e;
} finally {
try {
if (in != null)
in.close();
if (out != null)
out.close();
} catch (IOException e) {
throw e;
}
}

原文地址:https://www.cnblogs.com/antis/p/5942969.html