一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - 詳解Java文件下載的幾種實現方式

詳解Java文件下載的幾種實現方式

2020-07-15 12:38CodingME! JAVA教程

這篇文章主要介紹了詳解Java文件下載的幾種實現方式,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Java文件下載的幾種方式,具體如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public HttpServletResponse download(String path, HttpServletResponse response) {
    try {
      // path是指欲下載的文件的路徑。
      File file = new File(path);
      // 取得文件名。
      String filename = file.getName();
      // 取得文件的后綴名。
      String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
 
      // 以流的形式下載文件。
      InputStream fis = new BufferedInputStream(new FileInputStream(path));
      byte[] buffer = new byte[fis.available()];
      fis.read(buffer);
      fis.close();
      // 清空response
      response.reset();
      // 設置response的Header
      response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
      response.addHeader("Content-Length", "" + file.length());
      OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
      response.setContentType("application/octet-stream");
      toClient.write(buffer);
      toClient.flush();
      toClient.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return response;
  }
 
  public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
    // 下載本地文件
    String fileName = "Operator.doc".toString(); // 文件的默認保存名
    // 讀到流中
    InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
    // 設置輸出的格式
    response.reset();
    response.setContentType("bin");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    // 循環取出流中的數據
    byte[] b = new byte[100];
    int len;
    try {
      while ((len = inStream.read(b)) > 0)
        response.getOutputStream().write(b, 0, len);
      inStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 
  public void downloadNet(HttpServletResponse response) throws MalformedURLException {
    // 下載網絡文件
    int bytesum = 0;
    int byteread = 0;
 
    URL url = new URL("windine.blogdriver.com/logo.gif");
 
    try {
      URLConnection conn = url.openConnection();
      InputStream inStream = conn.getInputStream();
      FileOutputStream fs = new FileOutputStream("c:/abc.gif");
 
      byte[] buffer = new byte[1204];
      int length;
      while ((byteread = inStream.read(buffer)) != -1) {
        bytesum += byteread;
        System.out.println(bytesum);
        fs.write(buffer, 0, byteread);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

 //支持在線打開文件的一種方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
    File f = new File(filePath);
    if (!f.exists()) {
      response.sendError(404, "File not found!");
      return;
    }
    BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
    byte[] buf = new byte[1024];
    int len = 0;
 
    response.reset(); // 非常重要
    if (isOnLine) { // 在線打開方式
      URL u = new URL("file:///" + filePath);
      response.setContentType(u.openConnection().getContentType());
      response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
      // 文件名應該編碼成UTF-8
    } else { // 純下載方式
      response.setContentType("application/x-msdownload");
      response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
    }
    OutputStream out = response.getOutputStream();
    while ((len = br.read(buf)) > 0)
      out.write(buf, 0, len);
    br.close();
    out.close();
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/ungshow/archive/2009/01/12/1374491.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲毛片网 | 日本阿v精品视频在线观看 日本xxx片免费高清在线 | 久久精品国产在热亚洲完整版 | 国产网站免费观看 | 热久久最新地址 | 蜜桃影像传媒破解版 | 91探花在线观看 | 大吊操 | 手机在线免费观看高清 | 国产欧美二区三区 | 美女18隐私羞羞视频网站 | 9总探花新品牛仔背带裤 | 国产成人99久久亚洲综合精品 | 午夜影院免费看 | 色综合91久久精品中文字幕 | 久久久无码精品亚洲A片软件 | 欧美日韩一区二区三在线 | 嫩草影院永久在线一二三四 | 把内裤拔到一边高h1v1 | 亚洲天天综合 | 激情艳妇之性事高h | 男女xxoo做爰猛烈动态一 | 热久久最新视频 | 男人日女人的b | 午夜爱情动作片P | 好逼天天有 | 欧美同性video| 国产午夜精品一区二区 | 四虎影视免费观看免费观看 | 楚乔传第二部免费完整 | 男同志与动人物zozotv | 国产aaaaa一级毛片 | 色综合久久六月婷婷中文字幕 | 久久视频在线视频观看精品15 | 青青热久久综合网伊人 | 91啦在线视频 | yellow片在线观看 | 小泽玛丽av无码观看 | 黄动漫车车好快的车车双女主 | 99在线视频精品 | 91混血大战上海双胞胎 |