spring mvc下载文件

23 篇文章 1 订阅
3 篇文章 0 订阅

@RequestMapping(value ="/downloadFile", method ={RequestMethod.GET,RequestMethod.POST},produces ="text/html;charset=UTF-8")

                   @ResponseBody

                   publicvoid download(@RequestParam(value="bzbg_wsm",defaultValue="", required=true) String bzbg_wsm,

                                     @RequestParam(value="bzbg_wslj",defaultValue="", required=true) String bzbg_wslj,

                                     HttpServletRequestrequest, HttpServletResponse response) throws UnsupportedEncodingException {

                                //bzbg_wsm=newString(bzbg_wsm.getBytes("iso-8859-1"),"utf-8");

                               response.addHeader("Content-Disposition","attachment;filename=" + new String(bzbg_wsm.getBytes("gb2312"), "ISO8859-1" ) );

                                FileInputStream fis=null;

                           OutputStream os=null;

                           byte bytes[]=new byte[1024];

                           int len=0;

                           try {

                                   fis=new FileInputStream(bzbg_wslj);

                                        os=response.getOutputStream();

                                        while((len=fis.read(bytes))>0){

                                                  os.write(bytes,0,len);

                                        }  

                            }catch (Exception e) {

                                     e.printStackTrace();

                            }finally{

                                     try{

                                               os.close();

                                               fis.close();

                                     }catch (Exception e2) {

                                               e2.printStackTrace();

                                     }

                            }       

                   }

@RequestMapping(value ="/downloadFile", method ={RequestMethod.GET,RequestMethod.POST},produces ="text/html;charset=UTF-8")

                   @ResponseBody

                   publicvoid download(@RequestParam(value="bzbg_wsm",defaultValue="", required=true) String bzbg_wsm,

                                     @RequestParam(value="bzbg_wslj",defaultValue="", required=true) String bzbg_wslj,

                                     HttpServletRequestrequest, HttpServletResponse response) throws UnsupportedEncodingException {

                                //bzbg_wsm=newString(bzbg_wsm.getBytes("iso-8859-1"),"utf-8");

                               response.addHeader("Content-Disposition","attachment;filename=" + new String(bzbg_wsm.getBytes("gb2312"), "ISO8859-1" ) );

                                FileInputStream fis=null;

                           OutputStream os=null;

                           byte bytes[]=new byte[1024];

                           int len=0;

                           try {

                                   fis=new FileInputStream(bzbg_wslj);

                                        os=response.getOutputStream();

                                        while((len=fis.read(bytes))>0){

                                                  os.write(bytes,0,len);

                                        }  

                            }catch (Exception e) {

                                     e.printStackTrace();

                            }finally{

                                     try{

                                               os.close();

                                               fis.close();

                                     }catch (Exception e2) {

                                               e2.printStackTrace();

                                     }

                            }       

                   }



页面传输中文乱码解决方式

页面wslj=encodeURIComponent(wslj);

后台:wslj=java.net.URLDecoder.decode(wslj,"UTF-8");

 

xmmc=newString(xmmc.trim().getBytes("ISO-8859-1"),"UTF-8");


 第一种:设置  response.setHeader("Content-Disposition","attachment; filename=" + java.net.URLEncoder.encode(fileName,"UTF-8"));这里将文件名编码成UTF-8的格式,就不会出现URL出错了。IE6下注意中文文字不能超过超过17个。

 

 第二种:设置response.setHeader( "Content-Disposition","attachment;filename="  + newString( fileName.getBytes("gb2312"), "ISO8859-1" ) );将中文名编码为ISO8859-1的方式。不过该编码只支持简体中文.

 

按照上诉方式,可以综合一下两种方式解决绝大部分中文问题。

 

 fileName =URLEncoder.encode(fileNameSrc,"UTF-8");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值