码迷,mamicode.com
首页 > 编程语言 > 详细

Java文件下载

时间:2014-10-23 16:05:00      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   java   for   sp   

 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     }

 

Java文件下载

标签:style   blog   http   color   io   os   java   for   sp   

原文地址:http://www.cnblogs.com/guohu/p/4045697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!