91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Servlet實現代理文件下載功能

發布時間:2020-09-18 23:06:37 來源:腳本之家 閱讀:175 作者:blue_jjw 欄目:編程語言

用戶向代理服務器發送請求,代理服務器從后端服務器上獲取文件,并返回給用戶
web.xml:

<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:

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

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

北票市| 剑川县| 汽车| 富顺县| 岳池县| 綦江县| 双牌县| 铜陵市| 温泉县| 河源市| 娱乐| 中方县| 吉木萨尔县| 昌图县| 广汉市| 延边| 洞头县| 高安市| 龙州县| 乌拉特前旗| 仙桃市| 石棉县| 荆门市| 华容县| 璧山县| 福安市| 屏边| 仁布县| 汉寿县| 全南县| 梨树县| 班戈县| 田林县| 徐州市| 庆城县| 拉萨市| 讷河市| 宝鸡市| 东山县| 隆安县| 龙江县|