下载文件到电脑本地

 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     }
原文地址:https://www.cnblogs.com/lhq1996/p/11895932.html