JAVA对外的insert接口
/*** 获取TOKEN
* @param appKey
* @param appPwd
* @return
*/
@GetMapping("/getToken")
@ApiOperation(value = "获取TOKEN", notes = "获取TOKEN")
public Map getToken(String appKey, String appPwd) {
Map result =new HashMap();
String str = appKey+"_"+appPwd;
String token ="";
String code = "0";
String msg ="appkey或appPwd非法!";
if("wzzhly".equals(appKey)&&"1qaz!QAZ".equals(appPwd)){
token = TokenUtils.getToken(str);
msg="token获取成功!";
code="1";
}
result.put("code",code);
result.put("token",token);
result.put("msg",msg);
return result;
}
@GetMapping("/getReportFlowD")
@ApiOperation(value = "上报-日", notes = "上报-日")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
this.doPost(request, response);
}
@PostMapping("/reportFlowD")
@ApiOperation(value = "上报-日", notes = "上报-日")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
Map responseMap = new HashMap();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
Enumeration<String> list = request.getHeaderNames();
HumanFlowDataD humanFlowDataD;
while (list.hasMoreElements()) {
String str = list.nextElement();
String str1 = request.getHeader(str);
if (str.equals("token")) {
String verifyTokenRes = verifyToken(str1);
if ("succ".equals(verifyTokenRes)) {
DataInputStream inputStream = null;
String strInputstream ="";
inputStream =new DataInputStream(request.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] by = new byte[2048];
int n;
while((n=inputStream.read(by))!=-1){
baos.write(by,0,n);
}
strInputstream = new String(baos.toByteArray());
inputStream.close();
baos.close();
JSONObject jsonObject=JSONObject.parseObject(strInputstream);
log.info("转化的json数据:" + jsonObject);
humanFlowDataD = new Gson().fromJson(jsonObject.toString(), HumanFlowDataD.class);
log.info("转化后的对象:" + humanFlowDataD.getFlow_name()+","+ humanFlowDataD.getFlow_date());
int i = humanFlowDataDService.insert(humanFlowDataD);
if (i > 0) {
responseMap.put("code", "1");
responseMap.put("msg", "日数据上传成功");
} else {
responseMap.put("code", "0");
responseMap.put("msg", "日数据上传失败");
}
} else {
responseMap.put("code", "0");
responseMap.put("msg", "TOKE校验失败:失败码" + verifyTokenRes);
}
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject(responseMap);
out.write(json.toString());
out.flush();
out.close();
}
}
}
//token
public class TokenEncryptUtils { // 编码密码,可自定义private static final String ENCODED_PASSWORD = "ouyangjun";
/**
* * 编码
* * @param str
* * @return
* */
public static String encoded(String str) {
return strToHex(encodedString(str, ENCODED_PASSWORD));
}
/** * 转换 * @param str
* * @param password
* * @return
* */
private static String encodedString(String str, String password) {
char[] pwd = password.toCharArray();
int pwdLen = pwd.length;
char[] strArray = str.toCharArray();
for (int i = 0; i < strArray.length; i++) {
strArray[i] = (char) (strArray[i] ^ pwd[i % pwdLen] ^ pwdLen);
}
return new String(strArray);
}
private static String strToHex(String s) {
return bytesToHexStr(s.getBytes());
}
private static String bytesToHexStr(byte[] bytesArray) {
StringBuilder builder = new StringBuilder();
String hexStr;
for (byte bt : bytesArray) {
hexStr = Integer.toHexString(bt & 0xFF);
if (hexStr.length() == 1) {
builder.append("0");
builder.append(hexStr);
} else {
builder.append(hexStr);
}
}
return builder.toString();
}
/**
* * 解码
* * @param str
* * @return
* */
public static String decoded(String str) {
String hexStr = null;
try {
hexStr = hexStrToStr(str);
} catch (Exception e) {
e.printStackTrace();
}
if (hexStr != null) {
hexStr = encodedString(hexStr, ENCODED_PASSWORD);
}
return hexStr;
}
private static String hexStrToStr(String hexStr) {
return new String(hexStrToBytes(hexStr));
}
private static byte[] hexStrToBytes(String hexStr) {
String hex;
int val;
byte[] btHexStr = new byte[hexStr.length() / 2];
for (int i = 0; i < btHexStr.length; i++) {
hex = hexStr.substring(2 * i, 2 * i + 2);
val = Integer.valueOf(hex, 16);
btHexStr[i] = (byte) val;
}
return btHexStr;
}
}
//token util
import java.util.HashMap;import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/** * token生成和校验
* */
public class TokenUtils {
private static Map<String, String> MAP_TOKENS = new HashMap<String, String>();
private static final int VALID_TIME = 60 * 60 * 60; // token有效期(秒)
public static final String TOKEN_ERROR = "FF"; // 非法
public static final String TOKEN_OVERDUE = "GQ"; // 过期
public static final String TOKEN_FAILURE = "SX"; // 失效
/**
* * 生成token
* @param str: 该字符串可自定义,在校验token时要保持一致
* @return
*/
public static String getToken(String str) {
String token = TokenEncryptUtils.encoded(getCurrentTime() + "," + str);
MAP_TOKENS.put(str, token);
return token;
}
/**
* * 校验token的有效性
* * @param token
* * @return
* */
public static String checkToken(String token) {
if (token == null) {
return TOKEN_ERROR;
}
try {
String[] tArr = TokenEncryptUtils.decoded(token).split(",");
if (tArr.length != 2) {
return TOKEN_ERROR;
} // token生成时间戳
int tokenTime = Integer.parseInt(tArr[0]); // 当前时间戳
int currentTime = getCurrentTime();
if (currentTime - tokenTime < VALID_TIME) {
String tokenStr = tArr[1];
String mToken = MAP_TOKENS.get(tokenStr);
if (mToken == null) {
return TOKEN_OVERDUE;
} else if (!mToken.equals(token)) {
return TOKEN_FAILURE;
}
return tokenStr;
} else {
return TOKEN_OVERDUE;
}
} catch (Exception e) {
e.printStackTrace();
}
return TOKEN_ERROR;
}
/**
* 获取当前时间戳(10位整数)
* */
public static int getCurrentTime() {
return (int) (System.currentTimeMillis() / 1000);
}
/**
* * 移除过期的token
* */
public static void removeInvalidToken() {
int currentTime = getCurrentTime();
for (Entry<String, String> entry : MAP_TOKENS.entrySet()) {
String[] tArr = TokenEncryptUtils.decoded(entry.getValue()).split(",");
int tokenTime = Integer.parseInt(tArr[0]);
if (currentTime - tokenTime > VALID_TIME) {
MAP_TOKENS.remove(entry.getKey());
}
}
}
/**
* * 测试
* * @param args
* */
// private static final String OPERATION_EXIT = "EXIT";
// public static void main(String[] args) {
// String str = "username_and_password";
// String token = TokenUtils.getToken(str);
// System.out.println("生成的token:"+token);
// System.out.println("输入token,EXIT/E 退出");
// //让程序一直运行
// Scanner scan = new Scanner(System.in);
// while(scan.hasNext()) {
// String in = scan.next().toString();
// if(OPERATION_EXIT.equals(in.toUpperCase())
// || OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())) {
// scan.close();
// System.out.println("您成功已退出!");
// break;
// }
// System.out.println("您输入token的值:"+in);
// // 校验token
// String checkToken = TokenUtils.checkToken(in);
// System.out.println("checkToken Result: " + checkToken);
// if (str.equals(checkToken)) {
// System.out.println("==>token verification succeeded!");
// }
// }
// }
}
参考链接:https://blog.csdn.net/weixin_42894393/article/details/82385812
https://blog.csdn.net/p812438109/article/details/86677329
以上是 JAVA对外的insert接口 的全部内容, 来源链接: utcz.com/z/390070.html