Java模拟新浪和腾讯自动登录并发送微博

Java模拟新浪和腾讯自动登录并发送微博功能分享给大家,供大家参考,具体内容如下

1.准备工作
只是登录无需申请新浪和腾迅的开发者账号,如果需要发送微博功能,需要申请一个新浪和腾迅的开发者账号,并添加一个测试应用。

过程请参考官方帮助文档,申请地址:新浪:http://open.weibo.com    腾迅:http://dev.t.qq.com/

我们需要的是App Key和App Secre及redirect_URI,源代码中已经包含了我申请的测试key,但由于限制直接用我的key你们的账号是无法登录成功的。

2.注意事项
 1)、需要注意的是应用的App Key和App Secre及redirect_URI,对应项目根目录下的config.properties配置文件中的
client_ID=1745656892
client_SERCRET=66056719c1d8ca7bcaf36f411217cefa
redirect_URI=www.baidu.com
redirect_URI由于只是测试用并没有直接的回调页面,所以这里随便填写一个地址就行了,但要注意与应用-高级设置里的“回调页面”一致。 
2)、代码中的测试账号需要要自己添加测试账号,新浪的在“应用信息-测试账号”;腾迅的在“权限控制-创建白名单”中。当然直接用 开发者账号也可以。
3)、发送微博引用了新浪的weibo4j-oauth2-beta2.1.1.zip,腾迅的Java_SDK_v1.2.1.7z。核心类在util包下。

3.关键代码
1)、新浪 

package org.utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpException;
import org.core.weibo.sina.Oauth;
import org.core.weibo.sina.Timeline;
import org.core.weibo.sina.http.AccessToken;
import org.core.weibo.sina.model.WeiboException;
import org.core.weibo.sina.weibo4j.util.WeiboConfig;
/***
 * 模拟自动登录并发微博
 * @author zdw
 *
 */
public class Sina {
 /***
 * 模拟登录并得到登录后的Token
 * @param username 用户名
 * @param password 密码
 * @return
 * @throws HttpException
 * @throws IOException
 */
 public static AccessToken getToken(String username,String password) throws HttpException, IOException
 {
  String clientId = WeiboConfig.getValue("client_ID") ;
  String redirectURI = WeiboConfig.getValue("redirect_URI") ;
  String url = WeiboConfig.getValue("authorizeURL");

  PostMethod postMethod = new PostMethod(url);
  //应用的App Key
  postMethod.addParameter("client_id",clientId);
  //应用的重定向页面
  postMethod.addParameter("redirect_uri",redirectURI);
  //模拟登录参数
  //开发者或测试账号的用户名和密码
  postMethod.addParameter("userId", username);
  postMethod.addParameter("passwd", password);
  postMethod.addParameter("isLoginSina", "0");
  postMethod.addParameter("action", "submit");
  postMethod.addParameter("response_type","code");
  HttpMethodParams param = postMethod.getParams();
  param.setContentCharset("UTF-8");
  //添加头信息
  List<Header> headers = new ArrayList<Header>();
  headers.add(new Header("Referer", "https://api.weibo.com/oauth2/authorize?client_id="+clientId+"&redirect_uri="+redirectURI+"&from=sina&response_type=code"));
  headers.add(new Header("Host", "api.weibo.com"));
  headers.add(new Header("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"));
  HttpClient client = new HttpClient();
  client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
  client.executeMethod(postMethod);
  int status = postMethod.getStatusCode();
  System.out.println(status);
  if (status != 302)
  {
  System.out.println("token刷新失败");
  return null;
  }
  //解析Token
  Header location = postMethod.getResponseHeader("Location");
  if (location != null)
  {
  String retUrl = location.getValue();
  int begin = retUrl.indexOf("code=");
  if (begin != -1) {
   int end = retUrl.indexOf("&", begin);
   if (end == -1)
   end = retUrl.length();
   String code = retUrl.substring(begin + 5, end);
   if (code != null) {
   Oauth oauth = new Oauth();
   try{
    AccessToken token = oauth.getAccessTokenByCode(code);
    return token;
   }catch(Exception e){
    e.printStackTrace();
   }
   }
  }
  }
 return null;
 }
 /**
 * 发微博
 * @param token 认证Token
 * @param content 微博内容
 * @return
 * @throws Exception
 */
 public static boolean sinaSendWeibo(String token,String content) throws Exception {
 boolean flag = false ;
 Timeline timeline = new Timeline();
 timeline.client.setToken(token);
 try
 {
  timeline.UpdateStatus(content);
  flag = true ;
 }
 catch (WeiboException e)
 {
  flag = false ;
  System.out.println(e.getErrorCode());
 }
 return flag;
 }

 public static void main(String[] args) throws Exception
 {
 AccessToken at = getToken("xxxx","xxx");
 sinaSendWeibo(at.getAccessToken(),"测试呢");
 }
}

2)、腾迅 

package org.utils;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Scanner;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.core.weibo.tencent.api.UserAPI;
import org.core.weibo.tencent.oauthv2.OAuthV2;
import org.core.weibo.tencent.oauthv2.OAuthV2Client;

/***
 * 腾迅自动登录并获取个人信息
 * @author zdw
 *
 */
public class Tencent
{
 public static final String HEXSTRING = "0123456789ABCDEF";
 public static OAuthV2 oAuth = new OAuthV2();
 private static HttpClient client = new DefaultHttpClient();
 // 初始oAuth应用信息
 public static void init(OAuthV2 oAuth)
 {
 oAuth.setClientId("801216331");
 oAuth.setClientSecret("ea71b26b0cbe5778cdd1c09ad17553a3");
 oAuth.setRedirectUri("http://www.tencent.com/zh-cn/index.shtml");
 }
 /**
 *
 * @param qq
 *      http://check.ptlogin2.qq.com/check?uin={0}&appid=15000101&r={1 }
 *      返回的第三个值
 * @param password
 *      QQ密码
 * @param verifycode
 *      验证码
 * @return 加密后的密码
 * @throws UnsupportedEncodingException
 * @throws Exception
 *
 */
 public static String GetPassword(String qq, String password,
  String verifycode) throws Exception
 {
 String P = hexchar2bin(md5(password));
 String U = md5(P + hexchar2bin(qq.replace("\\x", "").toUpperCase()));
 String V = md5(U + verifycode.toUpperCase());
 return V;
 }

 public static String md5(String originalText) throws Exception
 {
 byte buf[] = originalText.getBytes("ISO-8859-1");
 StringBuffer hexString = new StringBuffer();
 String result = "";
 String digit = "";
 try
 {
  MessageDigest algorithm = MessageDigest.getInstance("MD5");
  algorithm.reset();
  algorithm.update(buf);
  byte[] digest = algorithm.digest();
  for (int i = 0; i < digest.length; i++)
  {
  digit = Integer.toHexString(0xFF & digest[i]);
  if (digit.length() == 1)
  {
   digit = "0" + digit;
  }
  hexString.append(digit);
  }
  result = hexString.toString();
 }
 catch (Exception ex)
 {
  result = "";
 }
 return result.toUpperCase();
 }

 public static String hexchar2bin(String md5str) throws UnsupportedEncodingException
 {
 ByteArrayOutputStream baos = new ByteArrayOutputStream(md5str.length() / 2);
 for (int i = 0; i < md5str.length(); i = i + 2)
 {
  baos.write((HEXSTRING.indexOf(md5str.charAt(i)) << 4 | HEXSTRING
   .indexOf(md5str.charAt(i + 1))));
 }
 return new String(baos.toByteArray(), "ISO-8859-1");
 }
 /***
 * 模拟登录
 * @param qq QQ号码
 * @param password QQ密码
 * @throws Exception
 */
 public static void login(String qq, String password) throws Exception
 {
 HttpGet get = new HttpGet("https://ssl.ptlogin2.qq.com/check?uin="+ qq + "&appid=46000101&ptlang=2052&js_type=2&js_ver=10009&r=0.7948186025712065");
 HttpResponse response = client.execute(get);
 String entity = EntityUtils.toString(response.getEntity());
 String[] checkNum = entity.substring(entity.indexOf("(") + 1,entity.lastIndexOf(")")).replace("'", "").split(",");
 String pass = "";
 String responseData = "";
 // 获取验证码(如果有验证码输出到C:/code.jpg,查看后输入可继续执行
 if ("1".equals(checkNum[0]))
 {
  // uin为qq号或者微博用户名
  HttpGet getimg = new HttpGet("http://captcha.qq.com/getimage?aid=46000101&r=0.3478789969909082&uin=" + qq + "&vc_type=" + checkNum[1] + "");
  HttpResponse response2 = client.execute(getimg);
  OutputStream os = new FileOutputStream("c:/code.jpg");
  byte[] b = EntityUtils.toByteArray(response2.getEntity());
  os.write(b, 0, b.length);
  os.close();
  Scanner in = new Scanner(System.in);
  responseData = in.nextLine();
  in.close();
 }
 else
 {
  responseData = checkNum[1];
 }
 /** *******************加密密码 ************************** */
 pass = GetPassword(checkNum[2], password, responseData);
 /** *********************** 登录 *************************** */
 HttpGet getimg = new HttpGet("https://ssl.ptlogin2.qq.com/login?ptlang=2052&u="+ qq+ "&p="+ pass+ "&verifycode="+ responseData+ "&aid=46000101&target=top&u1=https%3A%2F%2Fopen.t.qq.com%2Fcgi-bin%2Foauth2%2Fauthorize%3Fclient_id%3D"
   + oAuth.getClientId()+ "%26response_type%3Dcode%26redirect_uri="+ oAuth.getRedirectUri()+ "&ptredirect=1&h=1&from_ui=1&dumy=&qlogin_param=abbfew=ddd&wording=%E6%8E%88%E6%9D%83&fp=loginerroralert&action=8-13-240977&g=1&t=1&dummy=&js_type=2&js_ver=10009");
 HttpResponse response2 = client.execute(getimg);
 HttpEntity httpentity = response2.getEntity();
 String entityxc = EntityUtils.toString(httpentity);
 System.out.println(entityxc);
 }

 /**
 *
 * 请求微博开放平台应用 返回登录授权页面,但是如果没有sessionKey的话永远登录不成功 sessionKey
 * 发现在返回的页面中一个input标签里放的url中有,所以要取到这个sessionKey 其实直接访问标签中的url就可以跳转
 *
 */
 public static String getUrl() throws ClientProtocolException, IOException
 {
 HttpGet getcode = new HttpGet("https://open.t.qq.com/cgi-bin/oauth2/authorize?client_id="+ oAuth.getClientId()+ "&response_type=code&redirect_uri="
     + oAuth.getRedirectUri()+ "&checkStatus=yes&appfrom=&g_tk&checkType=showAuth&state=");
 HttpResponse response3 = client.execute(getcode);
 HttpEntity entityqqq = response3.getEntity();
 String entityxcc = EntityUtils.toString(entityqqq);
 String form = entityxcc.substring(entityxcc.indexOf("<form"), entityxcc
  .indexOf("</form>"));
 String[] ss = form.split("/>");
 String input = "";
 for (int i = 0; i < ss.length; i++)
 {
  if (ss[i].indexOf("name=\"u1\"") > 0)
  {
  input = ss[i];
  }
  ;
 }
 return input.substring(input.indexOf("value=\"") + 7, input.indexOf("\" type=\""));
 }
 /**
 * 解析并设置Token
 * @param get
 * @throws Exception
 */
 public static void setToken(HttpGet get) throws Exception
 {
 HttpResponse response4 = client.execute(get);
 HttpEntity entityqqq1 = response4.getEntity();
 String getUrlcode = EntityUtils.toString(entityqqq1);
 // 返回了最终跳转的页面URL,也就是回调页redirect_uri,页面地址上包含code openid openkey
 // 需要将这三个值单独取出来再拼接成 code=xxxxx&openid=xxxxx&openkey=xxxxxx的形式
 String entity = getUrlcode.substring(getUrlcode.indexOf("url="),getUrlcode.indexOf("\">"));
 StringBuffer sb = new StringBuffer();
 String[] arr = entity.split("\\?")[1].split("&");
 for (int x = 0; x < arr.length; x++)
 {
  if (arr[x].indexOf("code") >= 0 || arr[x].indexOf("openid") >= 0
   || arr[x].indexOf("openkey") >= 0)
  {
  sb.append(arr[x] + "&");
  }
  ;
 }
 // 利用code获取accessToken
 OAuthV2Client.parseAuthorization(sb.substring(0, sb.length() - 1), oAuth);
 oAuth.setGrantType("authorize_code");
 OAuthV2Client.accessToken(oAuth);
 }
 /***
 * 调用(腾迅开放平台账户接口)获取一个人的信息
 * @throws Exception
 */
 public static void getInfo() throws Exception
 {
 //输出Token,如果拿到了Token就代表登录成功,并可以进行下一步操作。
 System.out.println("Token="+oAuth.getAccessToken());
 UserAPI getuser = new UserAPI(oAuth.getOauthVersion());
 String userJson = getuser.otherInfo(oAuth, "json", "", oAuth.getOpenid());
 JSONObject userJsonObject = JSONObject.fromObject(userJson);
 Integer errcode = (Integer) userJsonObject.get("errcode");
 if (errcode == 0)
 {
  JSONObject userdataJsonObject = (JSONObject) userJsonObject.get("data");
  System.out.println(userdataJsonObject.toString());
 }
 }

 public static void main(String[] args) throws Exception
 {
 init(oAuth);
 login("123145", "xxxx");
 HttpGet get = new HttpGet(getUrl());
 setToken(get);
 getInfo();
 }

}

4.发送成功都有对应的日志输出
新浪(最后一行日志):

2078 DEBUG [2013-03-14 16:35:29]  {"created_at":"Thu Mar 14 16:35:30 +0800 2013","id":3555791132949940,"mid":"3555791132949940","idstr":"3555791132949940","text":"测试呢","source":"...

腾迅:

登录成功的日志标志: 
ptuiCB('0','0','https://open.t.qq.com/cgi-bin/oauth2/authorize?client_id=801216331&response_type=code&redirect_uri=http:','1','登录成功!', 'ㄗs:ヤ淡 啶');查看个人信息成功后的日志标志:

QHttpClient httpGet [3] Response = {"data":{"birth_day":26,"birth_month":8,"birth_year":2011,"city_code":"2","comp":null,"country_code":"1","edu":null,"email":"","exp":141,"fansnum":..

日志未全列出,只是作为参考。

源码下载:http://xiazai.jb51.net/201607/yuanma/sinaAndTencent(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • JavaWeb使用Cookie模拟实现自动登录功能(不需用户名和密码)

    其中包含两个jsp文件,分别为login.jsp和index.jsp 代码如下: login.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "

  • java验证用户是否已经登录 java实现自动登录

    本文为大家分享了java验证用户是否已经登录与实现自动登录的详细代码,供大家参考,具体内容如下 1.验证用户是否已经登录 package cn.hongxin.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletExceptio

  • Java传入用户名和密码并自动提交表单实现登录到其他系统的实例代码

    不用单点登录,模拟远程项目的登录页面表单,在访问这个页面的时候自动提交表单到此项目的登录action,就可以实现登录到其他系统. ssh框架项目 1.以下是本地系统的action代码: import java.io.IOException; import java.util.List; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.i

  • java中使用Filter控制用户登录权限具体实例

    学jsp这么长时间,做的项目也有七八个了,可所有的项目都是用户登录就直接跳转到其拥有权限的页面,或者显示可访问页面的链接.使用这种方式来幼稚地控制访问权限.从来没有想过如果我没有登录,直接输入地址也可以直接访问用户的页面的. 在jsp中权限的控制是通过Filter过滤器来实现的,所有的开发框架中都集成有Filter,如果不适用开发框架则有如下实现方法: LoginFilter.java 复制代码 代码如下: public class LoginFilter implements Filter {

  • 详解JavaEE使用过滤器实现登录(用户自动登录 安全登录 取消自动登录黑用户禁止登录)

    在我们生活中,对于账户的自动登录已经很常见了,所以利用过滤器实现这个功能. 主要介绍用户的自动登录和取消自动登录,以及实现一天自动登录或者n天实现自动登录,当用户ip被加入到黑名单之后,直接利用过滤器返回一个警告页面. 过滤器的功能很是强大,我们只需要在写好的前台后servlet之后进行添加就可以实现这个功能 Ps:这个仅仅只是一个演示而已,里面的访问数据库的部分,自己随意模拟了下,主要是突出实现自动登录的功能. 前台代码: 前台代码是成功与否都在这个页面显示.用到的技术:jstl标签的应用,s

  • java实现用户自动登录

    自动登录,是为了帮助用户多次使用这个网页时,不用再次输入用户名和密码就可以登录. 自动登录是指用户将用户的登录信息,人,保存到本地的文件中Cookie中. Name,value -声明时 new Cookie(key,value); Path-默认值,即为当前保存cookie的这个serlvet所在的路径. 如果Cookie在这样的路径:http://loclhost:8080/project/abc/AServlet 则Cookie的路径为: http://loclhost/project/a

  • java 验证用户是否已经登录与实现自动登录方法详解

    验证用户是否已经登录 package cn.hongxin.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import jav

  • JavaWeb开发使用Cookie创建-获取-持久化、自动登录、购物记录、作用路径

    1.cookie是啥?随手百度了网友的说说 简单的说,Cookie就是服务器暂存放在你计算机上的一笔资料,好让服务器用来辨认你的计算机.当你在浏览网站的时候,Web服务器会先送一小小资料放在你的计算机上,当下次你再光临同一个网站,Web服务器会先看看有没有它上次留下的Cookie资料,有的话,就会依据Cookie里的内容来判断使用者,送出特定的网页内容给你. 2.cookie在哪里? 3.cookie可以删除吗? 4.cookie实现原理 第一次请求浏览器,在浏览器的cookie存储区,没有co

  • java使用Filter实现自动登录的方法

    本文实例为大家分享了java实现自动登录的具体代码,供大家参考,具体内容如下 当你勾选(记住登录状态),用cookie保存用户名和密码.不勾选,cookie失效. 所有的页面都要经过autoLoginFilter.java 的过滤器,在这类中,必须要判断cookies不为null,获得所有的cookie,得到name为user的cookie,进行用户名和密码的验证,如果不为null,则将user存入session. 在LoginServlet.java中,获得username和password参

  • Java模拟新浪和腾讯自动登录并发送微博

    Java模拟新浪和腾讯自动登录并发送微博功能分享给大家,供大家参考,具体内容如下 1.准备工作 只是登录无需申请新浪和腾迅的开发者账号,如果需要发送微博功能,需要申请一个新浪和腾迅的开发者账号,并添加一个测试应用. 过程请参考官方帮助文档,申请地址:新浪:http://open.weibo.com    腾迅:http://dev.t.qq.com/ 我们需要的是App Key和App Secre及redirect_URI,源代码中已经包含了我申请的测试key,但由于限制直接用我的key你们的账

  • js实现将选中内容分享到新浪或腾讯微博

    微博如火如荼,大家都选择用微博带来社会化流量,顺便推广产品和网站,几乎所有的网站都有分享到代码,但是还有一种更快捷的分享方式,javascript就可以实现将选定内容轻松分享到新浪微博和腾讯微博,效果图如下: 将选中的内容分享到新浪微博,腾讯微博实现js代码如下: <STYLE> .img_sina_share { DISPLAY: none; CURSOR: pointer; POSITION: absolute } .img_qq_share { DISPLAY: none; CURSOR

  • Java调用新浪api通过Ip查询地区

    代码如下 import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import javax.servlet.http.HttpServletRequest; public class sinaIpUtil { public static void main(

  • JAVA爬虫实现自动登录淘宝

    目的 想通过JAVA代码实现淘宝网的自动登录,通过获取设置的登录信息自动填写并提交.目前这个代码是小编测试过的,可以通过,后期不知道淘宝会不会有相应的封堵策略. 代码分享: package util; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.fi

  • java实现微博后台登录发送微博

    首先你需要有个微博开发者账号,我们需要的是App Key和App Secre及redirect_URI,公司原本就有所以这一步 省下来了,百度搜下有很多. 好了现在开始: 首先我在网上找到了这个代码,这里写链接内容 本来挺高兴的一次性解决了嘛:可是报错 "token刷新失败",看了下代码 返回的是 200 不是302 ,额有点懵了,不管了先研究研究为什么会这样吧,我将他生成的网址放到浏览器上看了下,进入的是授权页面,不会需要先登录.F12 看了下确实也是 200 ,于是我估计是不是因为

  • 腾讯与新浪的通过IP地址获取当前地理位置(省份)的接口

    腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array("61.135.152.194","","北京市",""); 新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 多地域测试方法:http://int.dpool.sina.com.cn/iplookup/ip

  • Java模拟HTTP Get Post请求 轻松实现校园BBS自动回帖

    本文实例为大家分享了Java模拟HTTP Get Post请求,校园BBS自动回帖功能,供大家参考,具体内容如下 设计思路 找到帖子链接的集合,最后面数字变化, 就可以得到不同的帖子 防止帖子发表会又被删了的情况, 进行判断帖子是否存在 遍历这个集合, 对每个链接做回帖的POST请求 重难点 Note: 回帖需要用户登录信息 一种是利用Cookie 另一种是进行模拟登录 本文采用前者 代码 代码比较简单,注意事项是找到自己的Cookie,赋给String yourCookeie就可以直接运行 主

  • JavaScript模拟实现新浪下拉菜单效果

    思考:首先在CSS布局上就出错了,导致后面设置JS时就有很大的问题 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport"

  • Java 模拟银行自助终端系统

    一. 本系统模拟银行用户使用ATM机开户.查询.存款.取款功能,要求使用java语言编程实现. 说明: 1. 对于数据输入异常,可使用java异常处理机制进行处理. 2. 评分将以功能实现与代码规范性相结合的方式进行考核. 3. 如果对项目需求有疑问,可以随时以QQ留言方式联系我进行咨询. 4. 国庆放假期间,每天都有老师在公司值班,10月4日是我在公司值班,10月7日正常上班,欢迎大家到公司来做项目. 二. 项目功能要求: 项目开始运行显示主菜单为: 银行自助终端系统 ************

  • Java计时新姿势StopWatch的使用方法详解

    目录 一.背景 二.spring 用法 2.1 初遇 2.2 源码 2.3 注意事项 三.apache 用法 四.java 中使用StopWatch来计算时间差 五.最后 一.背景 有时我们在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,一般我们检测某段代码执行的时间,都是以如下方式来进行的: public static void main(String[] args) { Long startTime = System.curr

随机推荐