zoukankan      html  css  js  c++  java
  • 下载文件到电脑本地

     1 public void downloadSystemHelp(){
     2         DataInputStream in = null;
     3         OutputStream out= null;
     4         File f=null;
     5         try {
     6             
     7             f = new File(this.getClass().getResource("/").getPath());
     8             String path = f.getAbsolutePath();
     9             String newPath = path.replace('\', '/');
    10             if(newPath.contains("apache")){
    11                 newPath = newPath.substring(0, newPath.indexOf("/apache"));
    12             }
    13             String dirName =newPath+"/SystemHelp/";      //获取自己创建的文件夹路径
    14             f = new File(dirName);
    15             if(!f.exists()){
    16                 f.mkdirs();
    17             }
    18             String fileNames[] = f.list();   //获取文件夹下的文件
    19             String resultFileName="";
    20             System.out.println("文件 is "+fileNames);
    21             resultFileName = fileNames[0];
    22             response.setCharacterEncoding("UTF-8");  
    23             response.setHeader("Content-disposition", "attachment; filename=" +  URLEncoder.encode(resultFileName,"UTF-8"));// 设定输出文件头
    24             response.setContentType("multipart/form-data");// 定义输出类型
    25             //输入流:本地文件路径
    26             f=new File(dirName+resultFileName);
    27             in = new DataInputStream( new FileInputStream(f));  
    28             //输出流
    29             out = response.getOutputStream();
    30             //输出文件
    31             int bytes = 0;
    32             byte[] bufferOut = new byte[1024];  
    33             while ((bytes = in.read(bufferOut)) != -1) {  
    34                 out.write(bufferOut, 0, bytes);  
    35             }  
    36             
    37         } catch (Exception e) {
    38             e.printStackTrace();    
    39         }finally{
    40             if(null != in) {
    41                 try {
    42                     in.close();
    43                 } catch (IOException e) {
    44                     e.printStackTrace();
    45                 }
    46             }
    47             if(null != out) {
    48                 try {
    49                     out.close();
    50                 } catch (IOException e) {
    51                     e.printStackTrace();
    52                 }
    53             }
    54         }
    55     }
     1 //js代码
     2 function download(){
     3         //后台方法、文件类型、文件名
     4         downloadTemplate('${pageContext.request.contextPath}/cardIssueVehicleInfo/exportVehicleInfo', 'filename', 'test');
     5     }
     6 
     7     /**
     8      * 用于下载导入模板时的影藏form表单的提交,采用post方式提交
     9      *
    10      */
    11     function downloadTemplate(action, type, value){
    12         var form = document.createElement('form');
    13         document.body.appendChild(form);
    14         form.style.display = "none";
    15         form.action = action;
    16         form.id = 'excel';
    17         form.method = 'post';
    18         
    19         var newElement = document.createElement("input");  
    20         newElement.setAttribute("type","hidden");  
    21         newElement.name = type;
    22         newElement.value = value;
    23         form.appendChild(newElement); 
    24         
    25         form.submit();
    26     }
  • 相关阅读:
    解锁 redis 锁的正确姿势
    PHP实现Redis单据锁,防止并发重复写入
    js笔记
    FormData使用方法详解
    jquery里用each遍历的值存到数组和字符串
    Sublime Text3 安装 CTags 插件出现乱码
    通过pd.to_sql()将DataFrame写入Mysql
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket
    pandas 从txt读取DataFrame&DataFrame格式化保存到txt
    pandas 取消读取csv时默认第一行为列名
  • 原文地址:https://www.cnblogs.com/lhq1996/p/11895932.html
Copyright © 2011-2022 走看看