web文件下载,路径或文件名中文乱码 博客分类: webjava
/**
* 附件下载
* @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();
}