java实现文件下载功能

第一种方法:

 1 public HttpServletResponse download(String path, HttpServletResponse response) {
 2     try {
 3       // path是指欲下载的文件的路径。
 4       File file = new File(path);
 5       // 取得文件名。
 6       String filename = file.getName();
 7       // 取得文件的后缀名。
 8       String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
 9  
10       // 以流的形式下载文件。
11       InputStream fis = new BufferedInputStream(new 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(); // 文件的默认保存名
34     // 读到流中
35     InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
36     // 设置输出的格式
37     response.reset();
38     response.setContentType("bin");
39     response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
40     // 循环取出流中的数据
41     byte[] b = new byte[100];
42     int len;
43     try {
44       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   }

第二种方法:

 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     int len = 0;
10  
11     response.reset(); // 非常重要
12     if (isOnLine) { // 在线打开方式
13       URL u = new URL("file:///" + filePath);
14       response.setContentType(u.openConnection().getContentType());
15       response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
16       // 文件名应该编码成UTF-8
17     } else { // 纯下载方式
18       response.setContentType("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   }

猜你喜欢

转载自www.cnblogs.com/xiaoyue1606bj/p/10985764.html