/**
* 下载文件
* filePath 下载路径
*/
@RequestMapping(value = "/downLoadFile")
@ResponseBody
@SecureResource("下载文件")
public void downLoadFile(String filePath) {
HttpServletResponse response = WebObjectsHolder.getResponse();
try {
fileNameByUrl = URLEncoder.encode(filePath, "UTF-8");
if(StringUtils.isBlank(fileNameByUrl)) {
fileNameByUrl = fileName;
}
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename=" + fileNameByUrl);
OutputStream outp = null;
FileInputStream in = null;
try {
outp = response.getOutputStream();
in = new FileInputStream(filePath);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
outp.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outp != null) {
try {
outp.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java实现文件下载
最新推荐文章于 2024-07-19 21:04:03 发布