| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1606 人关注过本帖
标题:Struts1+hibernate框架中 Java代码下载文件 页面不显示下载框
只看楼主 加入收藏
飞龙在田
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-22
收藏
 问题点数:0 回复次数:1 
Struts1+hibernate框架中 Java代码下载文件 页面不显示下载框
代码如下:使用的是ajax请求
public void file(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){
        
//        HttpServletResponse response =  ServletActionContext.getResponse();
//        String na = ServletActionContext.getServletContext().getRealPath("/");
        String na = request.getSession().getServletContext().getRealPath("/");
        String n ="1.jpg";
        String m = "图片.jpg";
        InputStream ins = null;
        ServletOutputStream outs = null;
        try{
            File file = new File(na+n);
            System.out.println(file.exists());
            if(file.exists()){
                response.reset();
                response.setContentType("application/octet-stream; charset=UTF-8");
                response.setHeader("Content-disposition",
                        "attachment;filename=" +
                        URLEncoder.encode(m, "utf-8"));
                response.setHeader("Content-Length", String.valueOf(file.length()));
               
                outs = response.getOutputStream();
                ins = new FileInputStream(file);
                byte[] b = new byte[1024 * 10];
                int len = 0;
               
                while ((len = ins.read(b)) != -1) {
                    outs.write(b, 0, len);
                }
                //return mapping.findForward("mssys_view");
               
                System.out.println(outs.toString());
            //return null;
            }
        }catch(Exception e){
            
        }finally{
            try {
                if (outs != null)
                    outs.close();
                if (ins != null)
                    ins.close();
            } catch (Exception e) {
                outs = null;
                ins = null;
            }
            
        }
        //return "file";
搜索更多相关主题的帖子: 下载文件 request public null Java 
2016-05-22 17:13
飞龙在田
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-22
收藏
得分:0 
可有大神帮忙解决
2016-05-22 17:17
快速回复:Struts1+hibernate框架中 Java代码下载文件 页面不显示下载框
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015839 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved