Servlet实现代理文件下载功能
程序员文章站
2024-02-19 19:24:04
用户向代理服务器发送请求,代理服务器从后端服务器上获取文件,并返回给用户
web.xml:
用户向代理服务器发送请求,代理服务器从后端服务器上获取文件,并返回给用户
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 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。