解决jsp下载没有进度条同时解决struts不在页面打开下载文件

本文提供了一个解决Struts框架中页面打开下载文件时遇到的中文乱码问题的方法,通过使用URL编码技术确保文件路径正确显示并正常下载。

http://hi.baidu.com/8_8_8_8_8_8/blog/item/1565d173ddbdd41a8701b09d.html


// 解决struts不在页面打开下载文件
public ActionForward testExecute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
   DynaValidatorForm powerForm = (DynaValidatorForm) form;
   // TODO Auto-generated method stub
   InputStream is=null;
         OutputStream os=null;
         String path = request.getParameter("path");
         /**//*解决中文乱码问题,设置后产生一个新的String对象此对象以改变了编码*/
         String newpath=URLEncoder.encode(path,"utf-8");
         File f = new File(servlet.getServletContext().getRealPath("")+"\\Magazine\\"+path);
         byte[] b=new byte[1024];
         int i=0;
             try {
                 is = new FileInputStream(servlet.getServletContext().getRealPath("")+"\\Magazine\\"+path);
                 os = response.getOutputStream();
                 /**//*在页面上弹出一个下在窗口*/
                 response.setContentType("application/x-msdownload");
                 /**//*设置报头信息,弹出窗口中显示的文件名    newpath*/
                 response.setHeader("Content-Disposition", "Disposition; filename="+newpath);
                 //设置文件大小
                 response.setContentLength((int) f.length());
                 /**//*具体的输入输出流操作*/
                 while((i=is.read(b))!=-1){
                     os.write(b, 0, i);
                     i=0;
                 }
                 os.flush();
             } catch (IOException e){
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }finally{
                 try{
                     os.close();
                     is.close();
                 } catch (IOException e){
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
     return null;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值