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
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 訪問JSP文件或者Servlet文件時(shí)提示下載的解決方法
- java基于servlet編寫上傳下載功能 類似文件服務(wù)器
- jsp實(shí)現(xiàn)Servlet文件下載的方法
- servlet簡(jiǎn)單實(shí)現(xiàn)文件下載的方法
- servlet實(shí)現(xiàn)文件下載的實(shí)用類分享
- Java Servlet簡(jiǎn)單實(shí)例分享(文件上傳下載demo)
- servlet監(jiān)聽實(shí)現(xiàn)統(tǒng)計(jì)在線人數(shù)功能 附源碼下載
- servlet實(shí)現(xiàn)文件上傳、預(yù)覽、下載、刪除功能
- servlet實(shí)現(xiàn)文件下載的步驟及說明詳解
- 簡(jiǎn)單實(shí)現(xiàn)Servlet文件下載功能
相關(guān)文章
java獲取當(dāng)前日期和時(shí)間的二種方法分享
這篇文章主要介紹了java獲取當(dāng)前日期和時(shí)間的二種方法,需要的朋友可以參考下2014-03-03
在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)的問題解決,文中通過示例代碼介紹的非常詳細(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,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Java中Date與String相互轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了Java中Date與String相互轉(zhuǎn)換方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
java實(shí)現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

