文件下载时 文件名乱码问题

public class Briefing_Download_By_Id_Controller extends Base_Controller{ // private Log log = LogFactory.getLog(this.getClass()); public ModelAndView _handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception, ExceptionUtil { String id=request.getParameter("fileId"); //获取fileId BriefingInfo briefing=this.briefingLogic.getBriefingInfoById(id); //根据fileId根据获取BriefingInfo String location = SystemParamConfigUtil.getParamValueByParam("basePath") + File.separator + briefing.getPath(); DownloadFileUtil.downloadFile(response, location, briefing.getTitle()+".doc"); return null; } }

public static void downloadFile(HttpServletResponse response,String fileNameWithPath, String fileName) { try { // 转码(UTF-8-->GB2312),现在环境下的编码是UTF-8,但服务器操作系统的编码是GB2312 fileNameWithPath = new String(fileNameWithPath.getBytes(), "GB2312"); // 下载文件时显示的文件名,一定要经过这样的转换,否则乱码 fileName = URLEncoder.encode(fileName, "GB2312"); fileName = URLDecoder.decode(fileName, "ISO-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } File file = new File(fileNameWithPath); FileInputStream fileinputstream = null; try { fileinputstream = new FileInputStream(file); long l = file.length(); int k = 0; byte abyte0[] = new byte[65000]; response.setContentType("application/x-msdownload"); response.setContentLength((int) l); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); while ((long) k < l) { int j; j = fileinputstream.read(abyte0, 0, 65000); k += j; response.getOutputStream().write(abyte0, 0, j); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(fileinputstream!=null){ fileinputstream.close(); } } catch (IOException e) { e.printStackTrace(); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值