注册 登录
编程论坛 JAVA论坛

Struts1+hibernate框架中 Java代码下载文件 页面不显示下载框

飞龙在田 发布于 2016-05-22 17:13, 1847 次点击
代码如下:使用的是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";
1 回复
#2
飞龙在田2016-05-22 17:17
可有大神帮忙解决
1