实例如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/** * 文件下载 * @param filePath 文件路径 * @param fileName 文件名称 */ public void download(String filePath,String fileName){ try { //支持中文 fileName = URLEncoder.encode(fileName,"UTF-8"); HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); response.reset(); response.setContentType(request.getServletContext().getMimeType(fileName)); response.setHeader("Content-Disposition", "attachment;filename="+fileName); InputStream in = new FileInputStream(filePath); OutputStream out = response.getOutputStream(); byte[] b = new byte[1024]; int length = 0; while((length = in.read(b)) != -1) { out.write(b,0,length); } in.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } |
以上这篇java 文件下载支持中文名称的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/ljl-blog/p/5718494.html