我现在要从tomcat或者apache容器的虚拟目录里面直接下载文件,这样很简单,直接通过url访问文件就可以了。
问题是我现在要做一个servlet限制下载的连接数,这样我就要获取每个下载过程的生命周期了,上网找了很久都找不到相关的资料,请问有大神能给我一点点提示吗?跪谢!
我大概的思路是:客户端发送请求访问servlet------servlet增加连接数变量------servlert判断是否超过限定连接数---重定向文件url供客户端下载
另外一个servlet检测下载的生命周期,下载中断或者完成就减少连接数变量
只建一个HashMap,在里面记录每个连接的唯一ID和最后心跳时间,然后每隔一段时间去检测距离现在的时间,如果超过某个限定值就判定它已经停止。
下面是写的代码,仅供参考:
108
package com.raymon.receivefile.servlets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.raymon.receivefile.utils.ReadResourceMapping;
//import org.apache.log4j.Logger;
public class downFileServlet extends HttpServlet{
private static final long serialVersionUID = -5142659230279721142L;
ReadResourceMapping readproperties = new ReadResourceMapping();
//记录下载数
static int countLive = 0;
//记录每个连接信息的最后心跳时间,用作判断该连接是否还在下载
static HashMap<String,Date> lastAliveMap = new HashMap<String, Date>();
//用于比较式储存需要删除的key
static List<String> lostList = new ArrayList<String>();
//初始化代码,开始定时检测,只执行一次
@Override
public void init() throws ServletException {
//判断哪个连接信息的最后心跳时间距离超时,用作判断该连接是否还在下载
System.out.println("调用init方法");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//遍历HashMap作判断
System.out.println("开始计算各个连接最后心跳时间到现在的时差,当前countLive:"+countLive);
Iterator it1 = lastAliveMap.entrySet().iterator();
while(it1.hasNext()){
Entry obj = (Entry) it1.next();
System.out.println("lastAliveMap:"+obj.getKey()+":"+obj.getValue());
//如果某个连接的最后心跳距离现在超过限制,说明已经没有在下载,移出连接并连接数减一
long l=(new Date()).getTime()-((Date)(obj.getValue())).getTime();
//如果某连接已停止,记录下key,countLive减一
System.out.println("连接于对应时间距离:"+obj.getKey()+":"+l/1000);
if(l/1000>10){
System.out.println("检测出该连接停止:"+"lastAliveMap:"+obj.getKey()+":"+obj.getValue());
lostList.add((String)obj.getKey());
System.out.println("-------下载数减一--------");
countLive--;
System.out.println("当前下载数:"+countLive);
}
}
//比较完后再删除已停止的下载连接
for(String lostKey : lostList){
lastAliveMap.remove(lostKey);
}
}
}, 7000,7000);// 7秒后,每隔7秒检测一次
super.init();
}
public void doGet(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
if(req.getParameter("clientId")!=null){ //如果是下载心跳请求
//获取该下载链接的唯一key
// String key = req.getParameter("clientId") + req.getParameter("fileName");
String key = req.getParameter("clientId");
//初始化记录
lastAliveMap.put(key, new Date());
System.out.println("记录了连接,打印Map--");
Iterator it3 = lastAliveMap.entrySet().iterator();
while(it3.hasNext()){
Entry obj = (Entry) it3.next();
System.out.println("lastAliveMap:"+obj.getKey()+":"+obj.getValue());
}
System.out.println("--打印map");
}else if(req.getParameter("over")!=null){ //如果是下载结束请求,可能不需要
// countLive--;
System.out.println("下载完成:"+countLive);
}else if(countLive<=5){ //如果是正常下载请求,这里控制下载数
countLive++; //增加下载数标记
System.out.println("开始下载!当前下载数:"+countLive);
//fileBasePath要改为从请求传入,统一配置
String fileBasePath = req.getParameter("fileBasePath");
System.out.println("fileBasePath:"+fileBasePath);
//System.out.println("newRealFileName:"+new String(fileName.getBytes("ISO-8859-1"), "utf-8"));
//重新构建下载url
String fileUrl = fileBasePath + req.getParameter("fileName");
System.out.println("fileUrl: " + fileUrl + " from " + req.getLocalAddr());
response.sendRedirect(fileUrl);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。