欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

web文件下载,路径或文件名中文乱码 博客分类: webjava  

程序员文章站 2024-02-21 17:47:16
...

 

/**

* 附件下载

* @param dlPath 文件绝对路径

* @throws Exception 

*/

public void dl(String dlFilePath) throws Exception{

FacesContext context = FacesContext.getCurrentInstance(); 

HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();

 

ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();

 

String fileFullName = servletContext.getRealPath(dlFilePath);

 

File file = new File(fileFullName);

if(file.isDirectory() || !file.isFile()) return;

response.setContentType("application/x-msdownload"); 

response.setCharacterEncoding("UTF-8");

response.setHeader("Content-Disposition", "attachment; filename=\"" + DataUtil.encodeFileName(file.getName())+"\"");

response.getOutputStream().write(DataUtil.fileToByte(file));

response.getOutputStream().flush();

context.responseComplete();

}