Micro-channel sharing signature back buffer token, using papers

完成签名后台之后,其实目前每天2000已经够用了,但还是完善一下。

Design is to add a determination, as access from the public No. token preservation period to two hours, i.e. 7200s, if it is fresh, to take in the file, if not fresh, to re-request the micro channel acquisition token, then the new token to a file and updates the token acquisition time.

其实就是把token和时间存一下,使用文件、数据库都可以实现,我用的是文件。

Let me talk about the file, I use the .propertiesfile, and then there are two key values, a tokenvalue used to store token, one is timeused to store the token to take time, I stored by System.currentTimeMillis();the number of milliseconds taken.
filetoken.properties

token=1111
time=22222

ToolsreadAndwirte,java

import java.util.Properties;
import java.util.Enumeration;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class readAndwrite {
	 //读取信息
	public static String readBykey(String filePath, String key) {
        Properties p = new Properties();
        try {
            InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
            p.load(in);
            String value = p.getProperty(key);
            return value;
        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    //写入信息
    public static void WriteBykey (String filePath, String Key, String Value) throws IOException {
        Properties p = new Properties();
        InputStream in = new FileInputStream(filePath);
        //从输入流中读取属性列表(键和元素对) 
        p.load(in);
        //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。  
        //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
        OutputStream out = new FileOutputStream(filePath);
        p.setProperty(Key, Value);
        //以适合使用 load 方法加载到 Properties 表中的格式,  
        //将此 Properties 表中的属性列表(键和元素对)写入输出流  
        p.store(out, "Update " + Key + " name");
    }
}

Since each person's business, it gives only examples example.javaidea is acquired and the current millisecond time token ms / Save 1000 phase is greater than 7200 to see.

public String example {
		//地址是绝对地址
		long getoldtime = Long.parseLong(readAndwrite.GetValueByKey("C:/token/token.properties","time"));
		long currentTimeMillis = System.currentTimeMillis();
		if((currentTimeMillis-getoldtime)/1000>7200){
			accessToken = getAccessToken();//你定义的请求微信服务器获取token的方法
			try {
				readAndwrite.WriteProperties("C:/token/token.properties","token", accessToken);
				readAndwrite.WriteProperties("C:/token/token.properties","time", String.valueOf(currentTimeMillis));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			accessToken=readAndwrite.GetValueByKey("C:/token/token.properties","token");
		}
		return token;
	}

Note that, when read and write in the transfer of address is an absolute address, and deployed to your local address on the server is different, we should pay attention to, or else 404.
Finally thanks: read examples written.
https://blog.csdn.net/qy20115549/article/details/73368611

Published 64 original articles · won praise 103 · views 90000 +

Guess you like

Origin blog.csdn.net/P_Doraemon/article/details/89678925