Servlet实现代理文件下载功能

 更新时间:2017年12月18日 09:34:43   作者:blue_jjw  
这篇文章主要为大家详细介绍了Servlet实现代理文件下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

用户向代理服务器发送请求,代理服务器从后端服务器上获取文件,并返回给用户
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

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

相关文章

  • java 反射 动态调用不同类的静态方法(推荐)

    java 反射 动态调用不同类的静态方法(推荐)

    下面小编就为大家带来一篇JAVA 反射 动态调用不同类的静态方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • Java编程基本概念

    Java编程基本概念

    本文主要介绍了Java编程的基本概念,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • jackson序列化和反序列化的应用实践指南

    jackson序列化和反序列化的应用实践指南

    这篇文章主要给大家介绍了关于jackson序列化和反序列化的应用实践指南,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • 详解java中String、StringBuilder、StringBuffer的区别

    详解java中String、StringBuilder、StringBuffer的区别

    这篇文章主要介绍了java中String、StringBuilder、StringBuffer的区别,文中讲解的很清晰,有对于这方面不太懂的同学可以研究下
    2021-02-02
  • Spring中BeanFactory和ApplicationContext的作用和区别(推荐)

    Spring中BeanFactory和ApplicationContext的作用和区别(推荐)

    这篇文章主要介绍了Spring中BeanFactory和ApplicationContext的作用和区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Springboot整合支付宝支付功能

    Springboot整合支付宝支付功能

    这篇文章主要介绍了Springboot整合支付宝支付功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • mybatis拦截器及不生效的解决方法

    mybatis拦截器及不生效的解决方法

    本文主要介绍了mybatis拦截器及不生效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • java中的hashCode方法小例子

    java中的hashCode方法小例子

    这篇文章主要介绍了java中的hashCode方法小例子,有需要的朋友可以参考一下
    2013-12-12
  • spring boot中配置hikari连接池属性方式

    spring boot中配置hikari连接池属性方式

    这篇文章主要介绍了spring boot中配置hikari连接池属性方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • ZooKeeper入门教程一简介与核心概念

    ZooKeeper入门教程一简介与核心概念

    本文是ZooKeeper入门系列教程,涵盖ZooKeeper核心内容,通过实例和大量图表,结合实战,帮助学习者理解和运用,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2022-01-01

最新评论