java implementation file download function

the first method:

. 1  public the HttpServletResponse downloads (String path, the HttpServletResponse Response) {
 2      the try {
 . 3        // path refers to a path to the file to be downloaded. 
4        File File = new new File (path);
 5        // get the file name. 
6        String filename = file.getName ();
 7        // suffix obtained the document. 
. 8        String EXT = filename.substring (filename.lastIndexOf () +. 1 "." ) .ToUpperCase ();
 . 9   
10        // a stream of download files. 
. 11        the InputStream FIS = new new BufferedInputStream ( new new the FileInputStream (path));
12       byte[] buffer = new byte[fis.available()];
13       fis.read(buffer);
14       fis.close();
15       // 清空response
16       response.reset();
17       // 设置response的Header
18       response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
19       response.addHeader("Content-Length", "" + file.length());
20       OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
21       response.setContentType("application/octet-stream");
22       toClient.write(buffer);
23       toClient.flush();
24       toClient.close();
25     } catch (IOException ex) {
26       ex.printStackTrace();
27     }
28     return response;
29   }
30  
31   public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
32     // 下载本地文件
33     String fileName = "Operator.doc".toString(); //The default name of the file to save
 34      // read the stream 
35      the InputStream inStream = new new the FileInputStream ( "C: /Operator.doc"); // store the file path
 36      // formatted output 
37 [      response.reset ();
 38 is      the response.setContentType ( "bin" );
 39      Response.AddHeader ( "the Content-Disposition", "Attachment; filename = \" "+ fileName +" \ "" );
 40      // cycle withdrawn stream data 
41 is      byte [] = B new new  byte [100 ];
 42 is      int len;
 43 is      the try {
 44 is        the while ((len = inStream.read(b)) > 0)
45         response.getOutputStream().write(b, 0, len);
46       inStream.close();
47     } catch (IOException e) {
48       e.printStackTrace();
49     }
50   }
51  
52   public void downloadNet(HttpServletResponse response) throws MalformedURLException {
53     // 下载网络文件
54     int bytesum = 0;
55     int byteread = 0;
56  
57     URL url = new URL("windine.blogdriver.com/logo.gif");
58  
59     try {
60       URLConnection conn = url.openConnection();
61       InputStream inStream = conn.getInputStream();
62       FileOutputStream fs = new FileOutputStream("c:/abc.gif");
63  
64       byte[] buffer = new byte[1204];
65       int length;
66       while ((byteread = inStream.read(buffer)) != -1) {
67         bytesum += byteread;
68         System.out.println(bytesum);
69         fs.write(buffer, 0, byteread);
70       }
71     } catch (FileNotFoundException e) {
72       e.printStackTrace();
73     } catch (IOException e) {
74       e.printStackTrace();
75     }
76   }

The second method:

 1 public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
 2     File f = new File(filePath);
 3     if (!f.exists()) {
 4       response.sendError(404, "File not found!");
 5       return;
 6     }
 7     BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
 8     byte[] buf = new byte[1024];
 9     int0 = len ;
 10   
. 11      response.reset (); // very important 
12 is      IF (IsOnline) { // Open Online embodiment 
13 is        the URL = U new new the URL ( "File: ///" + filePath);
 14        the response.setContentType ( u.openConnection () the getContentType ());.
 15        response.setHeader ( "the Content-Disposition", "inline; filename =" + f.getName ());
 16        // filename should. 8-encoded into UTF 
. 17      } the else { // pure download mode 
18 is        the response.setContentType ( "file application / X-msdownload" );
 . 19       response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
20     }
21     OutputStream out = response.getOutputStream();
22     while ((len = br.read(buf)) > 0)
23       out.write(buf, 0, len);
24     br.close();
25     out.close();
26   }

 

Guess you like

Origin www.cnblogs.com/xiaoyue1606bj/p/10985764.html