当前位置: 代码迷 >> Web前端 >> web文件上载,路径或文件名中文乱码
  详细解决方案

web文件上载,路径或文件名中文乱码

热度:131   发布时间:2012-10-21 09:00:07.0
web文件下载,路径或文件名中文乱码

?

/**

* 附件下载

* @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();

}

  相关解决方案