zoukankan      html  css  js  c++  java
  • 文件下载

     下载图片或者文件有那么几种方法,下面详细总结。

    1,js方法

    [javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. function downloadFile(url){   
    2.     var elemIF = document.createElement("iframe");    
    3.     elemIF.src = url;    
    4.     elemIF.style.display = "none";    
    5.     document.body.appendChild(elemIF);    
    6.  }   

        直接在js中调用这个方法就可以实现下载,下载文件显示在浏览器下面。

       不过这种实现方法有一个问题,单纯的一个jsp页面或者html页面,直接使用这个方法是没有问题的,但是在程序开发的过程中往往就不那么单纯,大多是 在弹出框中嵌套页面,如果下载方法在这个嵌套的页面中就可能会出现问题,可能会出现文件不下载,但是后台和浏览器中都没有报错,就是不下载。

       出现这个原因就是下载页面嵌套过来后不是在iframe中。最简单的解决方法就是嵌套页面的时候用iframe,不过这种也不简单,如果用弹出框的形式的话,就麻烦至极了,不过还好,还有其他的方法实现下载。

     

     

    2,js+后台

    1. js:  
    2. //下载图片  
    3. function downloadFileT(path, fname){  
    4.     $('#fileDownFrame').form('submit', {  
    5.         url :'<%=request.getContextPath()%>/down/downLoadFile.action?filename='+encodeURI(encodeURI(fname))+'&filePath='+encodeURI(encodeURI(path)),  
    6.         dataType : 'text/xml',  
    7.         async:false,  
    8.         success : function(data) {  
    9.              。。。。。  
    10.         }  
    11.     });  
    12.  }   
    13.   
    14. html:  
    15.  <form id="fileDownFrame" style="display:none; visibility:hidden;" method="post"></form>  
    16.   
    17.   
    18. action:  
    19. /** 
    20.      * 下载附件 
    21.      * @param request 
    22.      * @param response 
    23.      * @throws UnsupportedEncodingException  
    24.      * @throws IOException  
    25.      */  
    26.     @Action("downLoadFile")  
    27.     public void downLoadFile() throws IOException {  
    28.         HttpServletRequest request=ServletActionContext.getRequest();  
    29.         HttpServletResponse response=ServletActionContext.getResponse();  
    30.         //response.setContentType("text/Xml;charset=utf-8");  
    31.         response.setContentType("application/octet-stream");  
    32.         JSONObject json=new JSONObject();  
    33.         try{  
    34.             String path= URLDecoder.decode(request.getParameter("filePath"),"utf-8");//从页面获取要下载的文件的相对路径  
    35.             String filename= URLDecoder.decode(request.getParameter("filename"),"utf-8");  
    36.             if(!"".equals(path)){  
    37.                 path=path.replaceAll("\\", "/");  
    38.                 File file=new File(request.getSession()  
    39.                         .getServletContext().getRealPath("/")+path);//构造要下载的文件    
    40.                 if(file.exists()){  
    41.                     InputStream ins=new FileInputStream(file);//构造一个读取文件的IO流对象  
    42.                     BufferedInputStream bins=new BufferedInputStream(ins);//放到缓冲流里面  
    43.                     OutputStream outs=response.getOutputStream();//获取文件输出IO流  
    44.                     BufferedOutputStream bouts=new BufferedOutputStream(outs);  
    45.                     response.setContentType("application/x-msdownload");//设置response内容的类型  
    46.                     response.addHeader("Content-Length", "" + file.length());  
    47.                     response.setHeader("Content-disposition","attachment;filename="+ new String( filename.getBytes("gb2312"), "ISO8859-1" ));//设置头部信息  
    48.                     int bytesRead = 0;  
    49.                     int size=(int)file.length();  
    50.                     byte[] buffer = new byte[size];  
    51.                     //开始向网络传输文件流  
    52.                     while ((bytesRead = bins.read(buffer, 0, buffer.length)) != -1) {  
    53.                         bouts.write(buffer, 0, bytesRead);  
    54.                     }  
    55.                     bouts.flush();//这里一定要调用flush()方法  
    56.                     ins.close();  
    57.                     bins.close();  
    58.                     outs.close();  
    59.                     bouts.close();  
    60.                    // json.put("result", "success");  
    61.                 }else{  
    62.                     response.reset();  
    63.                     json.put("result", "none");  
    64.                     response.getWriter().print(json.toString());  
    65.                     System.out.println("下载的文件不存在");  
    66.                 }  
    67.             }else{  
    68.                 json.put("result", "wrong");  
    69.                 response.getWriter().print(json.toString());  
    70.                 System.out.println("下载文件时参数错误");  
    71.             }  
    72.         }catch (Exception e) {  
    73.             json.put("result", "error");  
    74.             response.getWriter().print(json.toString());  
    75.             e.printStackTrace();  
    76.         }  
    77.     }  

    原文出自:http://blog.csdn.net/lishuangzhe7047/article/details/43560447

  • 相关阅读:
    Charles 手机抓包HTTPS设置以及证书安装
    Charles 抓包配置
    charles Windows 安装
    charles 过滤指定域名
    charles 手机证书下载安装
    charles 手机抓包设置
    charles overvoew
    charles 主界面总结
    charles 右键菜单
    获取近一周,近两周,本月,上个月
  • 原文地址:https://www.cnblogs.com/challengeof/p/4281852.html
Copyright © 2011-2022 走看看