欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Servlet實(shí)現(xiàn)代理文件下載功能

 更新時(shí)間:2017年12月18日 09:34:43   作者:blue_jjw  
這篇文章主要為大家詳細(xì)介紹了Servlet實(shí)現(xiàn)代理文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用戶向代理服務(wù)器發(fā)送請(qǐng)求,代理服務(wù)器從后端服務(wù)器上獲取文件,并返回給用戶
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; 
 
/** 
 * 給靜態(tài)大文件提供服務(wù)的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(); 
  //請(qǐng)求的文件名 
  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"); 
  //后端服務(wù)器的IP 
  String serverIP = "192.168.101.190"; 
  //后端服務(wù)器的文件地址 
  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 
 } 
 
} 

參考:一個(gè)文件下載的Servlet

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java獲取當(dāng)前日期和時(shí)間的二種方法分享

    java獲取當(dāng)前日期和時(shí)間的二種方法分享

    這篇文章主要介紹了java獲取當(dāng)前日期和時(shí)間的二種方法,需要的朋友可以參考下
    2014-03-03
  • Java由淺入深全面講解方法的使用

    Java由淺入深全面講解方法的使用

    方法,也稱函數(shù),如果想要重復(fù)一段或者多段代碼塊的使用,可以將這些代碼封裝成一個(gè)方法,方法具體表現(xiàn)為某種行為,使用方法可以提高代碼的復(fù)用性
    2022-04-04
  • 在SpringBoot中如何利用Redis實(shí)現(xiàn)互斥鎖

    在SpringBoot中如何利用Redis實(shí)現(xiàn)互斥鎖

    當(dāng)我們利用Redis存儲(chǔ)熱點(diǎn)數(shù)據(jù)時(shí),突然就過期失效或者被刪除了,導(dǎo)致大量請(qǐng)求同時(shí)訪問數(shù)據(jù)庫,增加了數(shù)據(jù)庫的負(fù)載,為減輕數(shù)據(jù)庫的負(fù)載我們利用互斥鎖,本文重點(diǎn)介紹在SpringBoot中如何利用Redis實(shí)現(xiàn)互斥鎖,感興趣的朋友一起看看吧
    2023-09-09
  • springboot 接口返回字符串帶引號(hào)的問題解決

    springboot 接口返回字符串帶引號(hào)的問題解決

    本文主要介紹了springboot 接口返回字符串帶引號(hào)的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 專屬于程序員的浪漫-Java輸出動(dòng)態(tài)閃圖iloveyou

    專屬于程序員的浪漫-Java輸出動(dòng)態(tài)閃圖iloveyou

    這篇文章主要介紹了專屬于程序員的浪漫-Java輸出動(dòng)態(tài)閃圖iloveyou,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Java中Date與String相互轉(zhuǎn)換的方法

    Java中Date與String相互轉(zhuǎn)換的方法

    這篇文章主要為大家詳細(xì)介紹了Java中Date與String相互轉(zhuǎn)換方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • java實(shí)現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具

    java實(shí)現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • mybatis-generator生成文件覆蓋問題的解決

    mybatis-generator生成文件覆蓋問題的解決

    這篇文章主要介紹了mybatis-generator生成文件覆蓋問題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Springboot內(nèi)嵌SQLite配置使用詳解

    Springboot內(nèi)嵌SQLite配置使用詳解

    這篇文章主要介紹了Springboot內(nèi)嵌SQLite配置使用詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • ElasticSearch的安裝與基本概念

    ElasticSearch的安裝與基本概念

    這篇文章主要介紹了ElasticSearch的安裝與基本概念,提供了一個(gè)分布式多用戶能力的全文搜索引擎,Elasticsearch是用Java開發(fā)的,需要的朋友可以參考下
    2023-04-04

最新評(píng)論