org.apache.http.client.HttpClient 访问服务器限速下载文件

org.apache.http.client.HttpClient 访问服务器限速下载文件

前端代码:

public static boolean doOutputStreamDownload(String fileName,String localPath,String remotePath) throws Exception{
        logger.info("doOutputStreamDownload( downloadPath : "+remotePath+", localPath : "+localPath+" , fileName : "+fileName+" )");
        if (StringUtil.isEmpty(remotePath)||StringUtil.isEmpty(localPath)||StringUtil.isEmpty(fileName)) {
            logger.error("getPackageOutputStream the parm is invalid!");
            return false;
        }

        UpdateToolRequestBean request=new UpdateToolRequestBean();
        request.setDownloadPath(remotePath+"/"+fileName);
        long downloadSpreed;
        try {
            downloadSpreed=Long.parseLong(SPREED);
        } catch (Exception e) {
            // TODO: handle exception
            logger.error("download spreed parse long exception ,set spreed=-1. ( "+e+" )");
            downloadSpreed=-1;
        }
        request.setSpreed(downloadSpreed);
        ObjectMapper objectMapper = JacksonHelper.objectMapper();
        String requestJsonStr = objectMapper.writeValueAsString(request);

        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
        org.apache.http.client.HttpClient httpclient = new DefaultHttpClient(httpParams);

        logger.info("http post url is "+URL);
        HttpPost httppost = new HttpPost(URL);      

        List<NameValuePair> formParams = new LinkedList<NameValuePair>();

        formParams.add(new BasicNameValuePair("data", requestJsonStr));
        formParams.add(new BasicNameValuePair("type", "getPackageStream"));

        HttpEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");

        httppost.setEntity(entity);

        HttpResponse response = httpclient.execute(httppost);

        if (response!=null) {
            int status=response.getStatusLine().getStatusCode();
            HttpEntity resEntity = response.getEntity();
            InputStream in = resEntity.getContent();
            if (status==200) {
                if (resEntity != null) {
                    logger.debug("in is " + in);
                    FileOutputStream os = null;
                    try {
                        File dir=new File(localPath);
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
                          os = new FileOutputStream(localPath+File.separator+fileName); 
                          byte[] buffer = new byte[1024];  
                          int read;  
                          while ((read = in.read(buffer)) > 0) {  
                              os.write(buffer, 0, read);  
                          }  
                          os.flush();
                          logger.info(fileName+" download success. ");
                          return true;
                    } catch (Exception e) {
                        // TODO: handle exception
                        logger.error("doOutputStreamDownload exception :  "+e);
                        throw new Exception("doOutputStreamDownload exception :  "+e);
                    }finally{
                        if (in!=null) {
                            in.close();
                        }
                        if (os!=null) {
                            os.close();
                        }
                        EntityUtils.consume(resEntity);

                    }


                }else{
                    logger.error("http entity is null. ");
                    return false;
                }
            }else{
                logger.error("service exec exception. "+inputStream2String(in));
                return false;
            }

        }else{
            logger.error("connect Service Exception! ");
            return false;
        }
    }

    private static String inputStream2String(InputStream in) throws IOException {
        //这里的编码规则要与上面的相对应
        BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
        String tempbf;
        StringBuffer out = new StringBuffer(4096);
        while ((tempbf = br.readLine()) != null) {
            out.append(tempbf);
        }
        return out.toString();
    }

服务器端代码:


public void doDownload(UpdateToolRequest sr,HttpServletResponse response){
        logger.info(remoteIP+" get package stream ["+sr.getDownloadPath()+"] .");
        long beginTime=System.currentTimeMillis();
        InputStream is=null;
        OutputStream os=null;
        try {
            os=response.getOutputStream();
            String filePath=sr.getDownloadPath();
            File file=new File(filePath);
            if (!file.exists()) {
                logger.error(remoteIP+" the package  ["+sr.getDownloadPath()+"] isn't exist.");
                response.setStatus(400);
                os.write((remoteIP+" the package  ["+sr.getDownloadPath()+"] isn't exist.").getBytes());
                return;
            }
            response.setStatus(200);

            is=new FileInputStream(file);
            int block=4*1024;
            byte[] buffer=new byte[block];
            long spreed=sr.getSpreed();
            int len;
            long size=0;
            long bTime;
            long nTime;
            double bSpreed;
            long outTime;
            while ((len = is.read(buffer)) > 0) {  
                bTime=System.currentTimeMillis();
                os.write(buffer, 0, len);
                size+=len;
                os.flush();
                Thread.sleep(1);
                nTime=System.currentTimeMillis();
                bSpreed=len*1000/(nTime-bTime);
                if (spreed!=-1&&spreed<bSpreed) {
                    //超速后计算线程等待时间
                    outTime=(long) (len*1000/spreed-len*1000/bSpreed);
                    //System.out.println((nTime-bTime)+" ,"+bSpreed+" ,"+spreed+" ,"+"sleep "+outTime);
                    Thread.sleep(outTime);
                }
            }  
            long endTime=System.currentTimeMillis();
            logger.info(remoteIP+" get package stream success in "+(endTime-beginTime)+" ms. the file "+filePath+" size : "+size+" Byte , spreed "+(size*1000/(endTime-beginTime))+"Byte/s.");

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(remoteIP+"get package info Exception : " +e);
            response.setStatus(400);
            try {os.write((remoteIP+" the package  ["+sr.getDownloadPath()+"] isn't exist.").getBytes());} catch (IOException e1) {}
        }finally{
            if (is!=null) {
                try {if (is!=null) {is.close();}} catch (IOException e) {logger.error(remoteIP+" inputstream close Exception : " +e);}
                try {if (os!=null) {os.close();}} catch (IOException e) {logger.error(remoteIP+" outputstream close Exception : " +e);}
            }
        }

    }
Tollbooth 是一个用 Go 语言编写的用来限制 HTTP 访问速度的中间件,可用来限制每个 HTTP 请求的传输速率。例如你可以不限制 / 的访问速率,但是可以针对 /login 限制每个 IP 每秒最多 POST 多少个请求。Go 程序中使用的方法:package main import (     "github.com/didip/tollbooth"     "net/http"     "time" ) func HelloHandler(w http.ResponseWriter, req *http.Request) {     w.Write([]byte("Hello, World!")) } func main() {     // You can create a generic limiter for all your handlers     // or one for each handler. Your choice.     // This limiter basically says: allow at most 1 request per 1 second.     limiter := tollbooth.NewLimiter(1, time.Second)     // This is an example on how to limit only GET and POST requests.     limiter.Methods = []string{"GET", "POST"}     // You can also limit by specific request headers, containing certain values.     // Typically, you prefetched these values from the database.     limiter.Headers = make(map[string][]string)     limiter.Headers["X-Access-Token"] = []string{"abc123", "xyz098"}     // And finally, you can limit access based on basic auth usernames.     // Typically, you prefetched these values from the database as well.     limiter.BasicAuthUsers = []string{"bob", "joe", "didip"}     // Example on how to wrap your request handler.     http.Handle("/", tollbooth.LimitFuncHandler(limiter, HelloHandler))     http.ListenAndServe(":12345", nil) 标签:Tollbooth
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值