大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用Java进行网速测试,命令格式:ping ip地址 -l 字节数。网速等于≈(发送的字节数/返回的时间[毫秒])K字节,以上计算的结果速为字节(``byte``),不是我们通常说的位(bps)。
铁山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
网络部署就是一个网络环境,用哪种方式把工作站连起来噻,包括布线、架构等等
2楼回答的不全面,我们公司做过相同功能的代码。
思路是并发上传和下载文件。
计算规单位时间内网卡吞吐量。(直接可以百度,会用到动态库,代码不少。贴补上来的)
如果只计算java的文件上传和下载。不能代表当前设备的网速。
原理: 到网上找个可以下载的apk 的URL,记住apk不要过大一般1M足够了.然后通过记录下载这个apk的时间和大小,算出当前网速.当然一个URL并不是很准确,要求精确的可以多下载几个
URL求平均值/span.
package com.panodic.settings.net;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.panodic.settings.util.LogUtil;
import com.panodic.settings.util.NetUtil;
import com.panodic.settings.util.Util;
import com.panodic.settings.view.PatchItem;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.panodic.settings.R;
public class MesureSpeed extends Activity implements OnClickListener {
private static final int LOADING = 0x111;
private static final int STOP = 0x112;
private ProgressBar mBar;
private int mProgressState;
private TextView mSpeed;
private Button mMeasureSpeed;
private PatchItem mBack;
private float mSpeedContent;
private String mAddr = "";
private String mAddr2 = "";
private String mAddr3 = "";
private Handler mHandler = new Handler(Util.sTaskRunner.getLooper());
private int testCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_mesure_speed);
mSpeed = (TextView) findViewById(R.id.speed_content);
mMeasureSpeed = (Button) findViewById(R.id.mesure_speed);
mBack = (PatchItem) findViewById(R.id.patch_settings_net_speed);
mBar = (ProgressBar) findViewById(R.id.bar);
mMeasureSpeed.setOnClickListener(this);
mBack.setOnClickListener(this);
testCount = 0;
}
@Override
public void onClick(View v) {
if (mBack.isMyChild(v)) {
Util.finish(this);
} else if (v == mMeasureSpeed) {
mMeasureSpeed.setEnabled(false);
mBar.setVisibility(View.VISIBLE);
mProgressState = 0;
testCount = 0;
mBar.setProgress(mProgressState);
mHandler.removeCallbacks(null);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
measureSpeed(mAddr);
}
}, 0);
}
}
private Handler mProgressHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case LOADING:
mBar.setProgress(mProgressState);
break;
case STOP:
mBar.setVisibility(View.GONE);
setSpeed();
mMeasureSpeed.setEnabled(true);
break;
default:
break;
}
}
};
private void setSpeed() {
if (mSpeedContent = 1024) {
mSpeedContent = (float) ((mSpeedContent) / (1024 + 0.0));
mSpeedContent = (float) (((int) (mSpeedContent * 10) % 10 + 0.0) / 10 + (int) mSpeedContent);
mSpeed.setText(mSpeedContent + getString(R.string.m));
} else {
mSpeed.setText((int) mSpeedContent + getString(R.string.kb));
}
}
private void measureSpeed(String httpUrl) {
if (!NetUtil.isWifiConnected(this) !NetUtil.isWireConnected(this)) {
Toast.makeText(this, getString(R.string.no_net), Toast.LENGTH_SHORT)
.show();
mProgressHandler.sendEmptyMessage(STOP);
return;
}
int fileLen = 0;
long startTime = 0;
long endTime = 0;
final String fileName = "tmp.apk";
HttpURLConnection conn = null;
InputStream is = null;
FileOutputStream fos = null;
File tmpFile = new File("/sdcard/temp");
if (!tmpFile.exists()) {
tmpFile.mkdir();
}
final File file = new File("/sdcard/temp/" + fileName);
try {
URL url = new URL(httpUrl);
try {
conn = (HttpURLConnection) url.openConnection();
LogUtil.d("lening");
fileLen = conn.getContentLength();
LogUtil.d("len=" + fileLen);
if (fileLen = 0) {
mSpeedContent = 0;
mProgressHandler.sendEmptyMessage(STOP);
Toast.makeText(this, getString(R.string.conn_fail),
Toast.LENGTH_SHORT).show();
return;
}
startTime = System.currentTimeMillis();
is = conn.getInputStream();
fos = new FileOutputStream(file);
byte[] buf = new byte[256];
conn.connect();
if (conn.getResponseCode() = 400) {
Toast.makeText(this, getString(R.string.no_time),
Toast.LENGTH_SHORT).show();
mProgressHandler.sendEmptyMessage(STOP);
return;
} else {
while (true) {
if (is != null) {
int numRead = is.read(buf);
if (numRead = 0) {
break;
} else {
fos.write(buf, 0, numRead);
}
mProgressState += (int) (((numRead + 0.0) / (fileLen + 0.0)) * 1000000);
mProgressHandler.sendEmptyMessage(LOADING);
// LogUtil.d("numRead=" + numRead + " fileLen="
// + fileLen);
} else {
break;
}
}
}
endTime = System.currentTimeMillis();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, getString(R.string.no_permission),
Toast.LENGTH_SHORT).show();
} finally {
if (conn != null) {
conn.disconnect();
}
try {
if (fos != null) {
fos.close();
}
if (is != null) {
is.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
mSpeedContent = fileLen / (endTime - startTime);
mProgressHandler.sendEmptyMessage(STOP);
}
}