开发者社区> 问答> 正文

请问怎么获取下载文件的生命周期啊?

我现在要从tomcat或者apache容器的虚拟目录里面直接下载文件,这样很简单,直接通过url访问文件就可以了。
问题是我现在要做一个servlet限制下载的连接数,这样我就要获取每个下载过程的生命周期了,上网找了很久都找不到相关的资料,请问有大神能给我一点点提示吗?跪谢!
我大概的思路是:客户端发送请求访问servlet------servlet增加连接数变量------servlert判断是否超过限定连接数---重定向文件url供客户端下载
另外一个servlet检测下载的生命周期,下载中断或者完成就减少连接数变量

展开
收起
落地花开啦 2016-05-31 14:18:49 2317 0
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

    只建一个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);
        } 
    }
    2019-07-17 19:21:53
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载