码迷,mamicode.com
首页 > Web开发 > 详细

ajax测网速

时间:2018-01-08 14:58:06      阅读:481      评论:0      收藏:0      [点我收藏+]

标签:tty   lang   ring   images   upload   fun   后台   getc   opacity   

ajax测下载速度

前台

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>测试网速</title>
</head>
<script type="text/javascript" src="./js/jquery-1.9.1.js"></script>
<script type="text/javascript">
     
    function Network(){
        checkSpeed();
    }
    
    function checkSpeed(){
        var a = window.setTimeout("ajaxLoad()",100);
    }
    
    function ajaxLoad( ){
        var param = {};
        var downPath = "<%=request.getContextPath()%>/TestSpeedServlet";
        var sumDownLoad = 0.000;
        var download = 0.000;
        var flag = true;
        if(flag){
            loop1:for(var i=0;i<10;i++){
                //测试下载速度
                var startDownLoadTime =new Date();
                $.ajax({
                    type:"post",
                    url :downPath,
                    async:false,
                    data: { "xmlDown": param},//1代表上传,2代表下载
                    success: function (data) {
                        var endDownTime =new Date();  
                        var a = data;
                        if(a.localeCompare("error")==0){
                            alert("测试网速的人数过多!请您稍后再试"); 
                            flag = false;
                            return loop1;
                        }else{
                            var usedDownTime = (endDownTime-startDownLoadTime)/1000;  //两个时间戳相差的毫秒数  
                            var dowmSpeed = 1000/usedDownTime;//1000Kb
                            sumDownLoad+=dowmSpeed;
                        }
                    },
                    error : function(data) {  
                         alert("测试网速的人数过多!请您稍后再试");  
                         flag = false;
                         return loop1;
                    }  
                });
            }
        }
        download = sumDownLoad/10;
        document.getElementById("downloadSpeed").value=download;
    }
    //数据的转换
    function transLoad(upload){
        if(upload>=1024){
            upload=upload/1024;
            upload = upload.toFixed(3);
            upload = upload+"MB/s"
        }else{
            upload = upload.toFixed(3);
            upload = upload+"K/s"
        }
        return upload
    }

</script>
<body>
    <style>
        #title {
           font-size: 25px;
           font-weight:bold;            
           font-family:"Microsoft Yahei";
        }
        .option{
            font-size:20px;
            font-family:"Microsoft Yahei";
        }
        
        td{
              border:1.5px solid gray;
          }
        #title{
             background-color:#a7c9f1;
        }
        
        .loading{width: 100%;filter:alpha(opacity=80); height: 100%;background-color:gray;position: fixed;top:0;left:0;bottom:0;right:0;z-index: 99;display: none;}
        .loading img{display: block;width: 400px;height: 200px;position: fixed;top:40%;left: 40%;margin-left: -75px;margin-top: -75px;}
    </style>
    
    <table width="400" border="5" align="center" >
           <tr>
             <td  colspan="2" align="center" id="title"><B>测试网速</B></td>
           </tr>
           <tr>
               <td class="option" id="upload" align="center">上传速度</td>
               <td><input type="text" name="uploadSpeed" id="uploadSpeed" size="25"/>
               </td>
           </tr>
           <tr>
               <td class="option" id="download" align="center">下载速度</td>
               <td><input  type="text" name="downloadSpeed" id="downloadSpeed" size="25" />
               </td>
           </tr>
           <tr>
             <td colspan="2" align="center" class="option">
                <input  type="button" name="testNetworkSpeed" value="测网速"  onClick="Network()" />
             </td>
           </tr>
     </table>
     <div class="loading" style="display: none;">
        <img src="../../../../images/ee.gif">
     </div>  
</body>
</html>

 

后台

package test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.json.JSONObject;

public class TestSpeedServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    static String testString = "";
    
    static {
        try {
            long sizeOf = FileUtils.sizeOf(new File("d:/temp3/eee.xml"));//eee.xml中为1024个英文字符
            testString = FileUtils.readFileToString(new File("d:/temp3/eee.xml"),"GBK");// windows
            // testString = FileUtils.readFileToString(new File("/tmp/bbb.txt"));//linux
            int fileSize = testString.length();
            System.out.println(String.format("文件大小%dByte,%d/1024=%fKb",fileSize,fileSize,fileSize/1024.0));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            JSONObject joStr = new JSONObject();
            joStr.put("result", testString);
            PrintWriter out = response.getWriter();
            out.print(joStr);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

}

 

ajax测网速

标签:tty   lang   ring   images   upload   fun   后台   getc   opacity   

原文地址:https://www.cnblogs.com/bobo1713930654/p/8242279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!