/**
* @Description: 下载文件
* @Author: vdi100
* @Param: fileName 文件名; pathName 文件路径
* @CreateDate: 2019/5/21 10:52
*/
public void testDownload(String fileName, String pathName, HttpServletResponse res) throws Exception {
res.setHeader("content-type" , "application/octet-stream" );
res.setContentType("application/octet-stream" );
res.setHeader("Content-Disposition" , "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8" ));
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File(pathName + fileName)));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}