标题: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-465134-1-1.html




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

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