Servlet实现代理文件下载功能

时间:2022-11-11 12:53:02

用户向代理服务器发送请求,代理服务器从后端服务器上获取文件,并返回给用户
web.xml:

?
1
2
3
4
5
6
7
8
<servlet>
 <servlet-name>BigFile</servlet-name>
 <servlet-class>cn.ac.dsp.servlet.BigFile</servlet-class>
</servlet>
 <servlet-mapping>
 <servlet-name>BigFile</servlet-name>
 <url-pattern>*.ts</url-pattern>
</servlet-mapping>

servlet:

?
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
package cn.ac.dsp.servlet;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
 
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.log4j.Logger;
 
import cn.ac.dsp.common.Constant;
import cn.ac.dsp.common.SystemParameters;
 
/**
 * 给静态大文件提供服务的servlet
 */
public class BigFile extends HttpServlet {
 private static final long serialVersionUID = 1L;
 private static final Logger log = Logger.getLogger(BigFile.class);
  
 /**
  * @see HttpServlet#HttpServlet()
  */
 public BigFile() {
  super();
  // TODO Auto-generated constructor stub
 }
 
 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  log.info("request for bigfile");
  long startTime = System.nanoTime();
  String requestUrl = request.getRequestURI();
  //请求的文件名
  String filename = requestUrl.substring(requestUrl.lastIndexOf("/"));
  HttpClient httpClient = new DefaultHttpClient();
  httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Constant.HttpConnTimeOut);
  httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Constant.SoConnTimeOut);
  httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
  //后端服务器的IP
  String serverIP = "192.168.101.190";
  //后端服务器的文件地址
  StringBuilder backUrl = new StringBuilder();
  backUrl.append("http://");
  backUrl.append(serverIP);
  backUrl.append("/LBA/bigfile/");
  backUrl.append(filename);
  HttpGet httpGet = new HttpGet(backUrl.toString());
  httpGet.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
  log.info("distribute bigfile to " + backUrl.toString());
  HttpResponse backResponse;
  try {
   backResponse = httpClient.execute(httpGet);
//   log.info(backResponse.getParams().getParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET));
   HttpEntity httpEntity = backResponse.getEntity();
   InputStream in = httpEntity.getContent();
//   BufferedReader br = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"));
   byte[] buf = new byte[4096];
   int readLength;
   response.setCharacterEncoding("UTF-8");
   ServletOutputStream out = response.getOutputStream();
   while((readLength = in.read(buf)) != -1){
    out.write(buf, 0, readLength);
   
   in.close();
   out.flush();
   out.close();
  } catch (ClientProtocolException e) {
   StringWriter sw = new StringWriter();
   e.printStackTrace(new PrintWriter(sw));
   log.error("ClientProtocolException when redirect bigfile. " + sw.toString()); 
  } catch (IOException e) {
   StringWriter sw = new StringWriter();
   e.printStackTrace(new PrintWriter(sw));
   log.error("IOException when redirect bigfile. " + sw.toString()); 
  }
  long endTime = System.nanoTime();
  System.out.println("Response time: " + (endTime-startTime) + " ns");
 }
 
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }
 
}

参考:一个文件下载的Servlet

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/blue_jjw/article/details/8774150