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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java Web端程序實現(xiàn)文件下載的方法分享

Java Web端程序實現(xiàn)文件下載的方法分享

2020-05-06 10:59死神的喪鐘 JAVA教程

這篇文章主要介紹了Java Web端程序實現(xiàn)文件下載的方法分享,包括一個包含防盜鏈功能的專門針對圖片下載的程序代碼示例,需要的朋友可以參考下

Web文件下載有兩種,一種是文件在網站目錄下,在瀏覽器中直接輸入文件路徑即可下載,如http://www.xxx.com/file.zip。另外一種是文件不在網站目錄下或者文件是動態(tài)生成的(導出報表或者導出excel等),這種情況需要通過response的OutputStream實現(xiàn)文件的下載。DownloadUtils是一個Java Web文件下載工具類,提供多種靜態(tài)方法實現(xiàn)文件下載。

?
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.rhui.util;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
 
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.lang3.StringUtils;
 
/**
 * 文件下載類
 */
public class DownloadUtils {
  /**
   * 文件下載編碼
   * 該編碼告訴瀏覽器文件名的編碼方式,以防下載中文文件名時有亂碼
   */
  private static String encoding = "utf-8";
   
  /**
   * 文件下載
   * @param response
   * @param filePath 文件在服務器上的路徑,包含文件名
   */
  public static void download(HttpServletResponse response, String filePath){
    File file = new File(filePath.toString());
    download(response, file, null, encoding);
  }
   
  /**
   * 文件下載
   * @param response
   * @param filePath 文件在服務器上的路徑,包括文件名稱
   * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數(shù)
   */
  public static void download(HttpServletResponse response, String filePath, String fileName){
    File file = new File(filePath.toString());
    download(response, file, fileName, encoding);
  }
   
  /**
   * 文件下載
   * @param response
   * @param filePath 文件在服務器上的路徑,包括文件名稱
   * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數(shù)
   * @param encoding 文件名稱編碼
   */
  public static void download(HttpServletResponse response, String filePath, String fileName, String encoding){
    File file = new File(filePath.toString());
    download(response, file, fileName, encoding);
  }
   
  /**
   * 文件下載
   * @param response
   * @param file 文件
   * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數(shù)
   */
  public static void download(HttpServletResponse response, File file) {
    download(response, file, null, encoding);
  }
   
  /**
   * 文件下載
   * @param response
   * @param file 文件
   * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數(shù)
   */
  public static void download(HttpServletResponse response, File file, String fileName) {
    download(response, file, fileName, encoding);
  }
   
  /**
   * 文件下載
   * @param response
   * @param file 文件
   * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數(shù)
   * @param encoding 文件名稱編碼
   */
  public static void download(HttpServletResponse response, File file, String fileName, String encoding) {
    if(file == null || !file.exists() || file.isDirectory()){
      return;
    }
     
    // 如果不指定文件下載到瀏覽器的名稱,則使用文件的默認名稱
    if (StringUtils.isBlank(fileName)) {
      fileName = file.getName();
    }
 
    try {
      InputStream is = new FileInputStream(file);
      download(response, is, fileName, encoding);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
   
  /**
   * 文件下載
   * @param response
   * @param is 文件輸入流
   * @param fileName 下載的文件名稱
   * @throws IOException
   */
  public static void download(HttpServletResponse response, InputStream is, String fileName){
    download(response, is, fileName, encoding);
  }
   
  /**
   * 文件下載
   * @param response
   * @param is 文件輸入流
   * @param fileName 下載的文件名稱
   * @param encoding 編碼格式
   */
  public static void download(HttpServletResponse response, InputStream is, String fileName, String encoding){
    if(is == null || StringUtils.isBlank(fileName)){
      return;
    }
     
    BufferedInputStream bis = null;
    OutputStream os = null;
    BufferedOutputStream bos = null;
     
    try{
      bis = new BufferedInputStream(is);
      os = response.getOutputStream();
      bos = new BufferedOutputStream(os);
      response.setContentType("application/octet-stream;charset=" + encoding);
      response.setCharacterEncoding(encoding);
      response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, encoding));
      byte[] buffer = new byte[1024];
      int len = bis.read(buffer);
      while(len != -1){
        bos.write(buffer, 0, len);
        len = bis.read(buffer);
      }
       
      bos.flush();
    }catch(IOException e){
      e.printStackTrace();
    }finally{
      if(bis != null){
        try{
          bis.close();
        }catch(IOException e){}
      }
       
      if(is != null){
        try{
          is.close();
        }catch(IOException e){}
      }
    }
  }
 
  public static String getEncoding() {
    return encoding;
  }
 
  public static void setEncoding(String encoding) {
    DownloadUtils.encoding = encoding;
  }
}

如果文件保存在服務器的非網站目錄下

?
1
2
String filePath = "c:\\file.zip";
DownloadUtils.download(response, filePath);

如果文件是輸入流

?
1
2
3
4
5
6
// is為文件輸入流
// fileName為瀏覽器下載的文件名稱
// encoding為文件名稱編碼,預防文件中有中文的時候產生亂碼
String fileName = "file.zip";
String encoding = "utf-8";
DownloadUtils.download(response, is, fileName, encoding);

Servlet中文件下載

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.rhui.web.servlet;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.rhui.util.DownloadUtils;
 
@WebServlet("/download/servlet")
public class DownloadServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
   
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filePath = "c:\\file.zip";
    DownloadUtils.download(response, filePath);
  }
 
}

 

PS:圖片下載(含防盜鏈功能)

?
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
package cn.itcast.day06.web.servlet;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class DownloadServlet extends HttpServlet {
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 
 // 實現(xiàn)防盜鏈功能
 // 獲得 referer 頭 用于說明來訪者來自哪里
 String referer = request.getHeader("referer");
 if(referer==null || !referer.startsWith("http://localhost")) {
  // 是盜鏈者
  response.sendRedirect("/day06/index.jsp");
  return ;
 }
 
 
 // 解決response中文亂碼問題
 response.setContentType("text/html;charset=utf-8"); // 設置消息體的編碼
 
 
 // 通過 http 協(xié)議 發(fā)送的http響應消息頭 不能出現(xiàn)中文 中文必須要經過url編碼
 String filename = URLEncoder.encode("美女.jpg", "utf-8");
 
 // 通知瀏覽器以下載的方式讀取資源
 response.setHeader("content-disposition", "attachment;filename="+filename);
 
 // 讀取圖片數(shù)據(jù) 發(fā)給ie瀏覽器
 String webPath = "/download/美女.jpg"; // 相當于當前web應用的path
 
 ServletContext servletContext = super.getServletContext();
 
 InputStream in = servletContext.getResourceAsStream(webPath);
 
 
 OutputStream out = response.getOutputStream();
 
 
 int len;
 byte[] buffer = new byte[1024];
 while((len=in.read(buffer))!=-1)
  out.write(buffer, 0, len);
 
 }
 
 public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 doGet(request, response);
 }
 
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男女啪啪gif | 99热视频| 女bbbbxxx孕妇 | 精品一区二区三区中文 | 亚洲一级片在线播放 | 奇米影视777最新在线 | 国产按摩系列 | 欧美一区不卡二区不卡三区 | 95在线观看精品视频 | 亚洲国产欧美在线人成aaa | 大学生宿舍飞机 free | 明星梦淫| 国产99精品成人免费视频 | 精品91一区二区三区 | 美女gif趴跪式抽搐动态图 | 亚欧成人中文字幕一区 | 午夜深情在线观看免费 | 人人干国产 | 四大美女思春艳史片 | 美女扒下内裤让男人桶的图片 | 校花在公车上被内射好舒服 | 亚洲欧美日韩国产一区二区精品 | 国产精品俺来也在线观看了 | 成人精品一区久久久久 | yy8090韩国日本三理论免费 | 糖心hd在线观看 | 成年人在线观看免费视频 | 午夜伦理:伦理片 | 国产一区二区三区欧美 | 国产精品51麻豆cm传媒 | 欧美摸胸 | 日本xxxx在线视频免费 | 国产成人lu在线视频 | 538亚洲欧美国产日韩在线精品 | 调教女帝| 91精品国产9l久久久久 | 美女张开双腿让男人捅 | 校花被强迫np肉高h 校服下的白嫩小乳尖h1v1 | 极品美女a∨片在线看 | 非洲一级毛片又粗又长aaaa | www.99热|