代码案例
package com.xiangxin.fangwei;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import weaver.systeminfo.SystemEnv;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.*;
//泛微离职人员清单
public class ListofResignationPersonnel {
public static void main(String[] args) throws UnsupportedEncodingException {
ListofResignationPersonnel restfulDemo = new ListofResignationPersonnel();
restfulDemo.doAction();
}
public static String getCurrentTime() {
Date newdate = new Date();
long datetime = newdate.getTime();
Timestamp timestamp = new Timestamp(datetime);
String currenttime = (timestamp.toString()).substring(11, 13) + ":" + (timestamp.toString()).substring(14, 16) + ":"
+ (timestamp.toString()).substring(17, 19);
return currenttime;
}
public static String getCurrentDate() {
Date newdate = new Date();
long datetime = newdate.getTime();
Timestamp timestamp = new Timestamp(datetime);
String currentdate = (timestamp.toString()).substring(0, 4) + "-" + (timestamp.toString()).substring(5, 7) + "-"
+ (timestamp.toString()).substring(8, 10);
return currentdate ;
}
/**
* 获取当前日期时间。 YYYY-MM-DD HH:MM:SS
* @return 当前日期时间
*/
public static String getCurDateTime() {
Date newdate = new Date();
long datetime = newdate.getTime();
Timestamp timestamp = new Timestamp(datetime);
return (timestamp.toString()).substring(0, 19);
}
/**
* 获取时间戳 格式如:19990101235959
* @return
*/
public static String getTimestamp(){
return getCurDateTime().replace("-", "").replace(":", "").replace(" ", "");
}
public static int getIntValue(String v, int def) {
try {
return Integer.parseInt(v);
} catch (Exception ex) {
return def;
}
}
public static String null2String(Object s) {
return s == null ? "" : s.toString();
}
/**
*restful接口调用案例
*以getModeDataPageList为例
*/
public void doAction() throws UnsupportedEncodingException {
CloseableHttpResponse response;// 响应类,
CloseableHttpClient httpClient = HttpClients.createDefault();
//restful接口url
HttpPost httpPost = new HttpPost("http://oa.luckyharvest.cn:8188/api/cube/restful/interface/saveOrUpdateModeData/dblzryqd");
String currentDate = getCurrentDate(); //当前日期
String currentTime = getCurrentTime();//当前时间
String currentTimeTamp = getTimestamp(); //获取时间戳
Map<String,Object> params =new HashMap<>();
Map<String,Object> paramDatajson = new HashMap<>();
Map<String,Object> header = new HashMap<>();
String systemid = "drzpg"; //系统标识
String d_password = "123"; //密码
header.put("systemid",systemid);
header.put("currentDateTime","20240926092206");
String md5Source = systemid+d_password+"20240926092206";
System.out.println("拼接结果:"+md5Source);
//String md5Source = systemid+d_password+currentTimeTamp;
String md5OfStr = getMD5Str(md5Source).toLowerCase();
header.put("Md5",md5OfStr); //Md5是:系统标识+密码+时间戳 并且md5加密的结果
paramDatajson.put("header",header);
//封装mainTable参数
System.out.println("header:"+header);
JSONObject mainTable = new JSONObject();
mainTable.put("lzryxm", "黎XX");
mainTable.put("gh", "Lh000001");
mainTable.put("lzrq", "2024-09-19");
mainTable.put("lzzt", 1);
mainTable.put("lzlx", 3);
//封装operationinfo参数
JSONObject operationinfo = new JSONObject();
operationinfo.put("operator", "8605");
JSONObject jsonObject=new JSONObject();
jsonObject.put("operationinfo",operationinfo);
jsonObject.put("mainTable",mainTable);
List data=new ArrayList();
data.add(jsonObject);
paramDatajson.put("data",data);
params.put("datajson",paramDatajson);
//装填参数
List nvps = new ArrayList();
if(params!=null){
for (Map.Entry entry :params.entrySet()) {
nvps.add(new BasicNameValuePair((String) entry.getKey(), JSONObject.toJSONString(entry.getValue())));
}
}
System.out.println("nvps:"+nvps);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
System.out.println("urlEncodedFormEntity:"+urlEncodedFormEntity);
try{
httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
response = httpClient.execute(httpPost);
if (response != null && response.getEntity() != null) {
//返回信息
String resulString = EntityUtils.toString(response.getEntity());
//todo这里处理返回信息
System.out.println("成功"+ resulString);
}else{
System.out.println("获取数据失败,请查看日志"+currentDate+" "+currentTime);
}
}catch (Exception e){
System.out.println("请求失败"+currentDate+" "+currentTime+"====errormsg:"+e.getMessage());
}
}
public String getMD5Str(String plainText){
//定义一个字节数组
byte[] secretBytes = null;
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
//对字符串进行加密
md.update(plainText.getBytes());
//获得加密后的数据
secretBytes = md.digest();
} catch (NoSuchAlgorithmException e) {
//throw new RuntimeException("没有md5这个算法!");
throw new RuntimeException(SystemEnv.getHtmlLabelName(517545,0));
}
//将加密后的数据转换为16进制数字
String md5code = new BigInteger(1, secretBytes).toString(16);
// 如果生成数字未满32位,需要前面补0
// 不能把变量放到循环条件,值改变之后会导致条件变化。如果生成30位 只能生成31位md5
int tempIndex = 32 - md5code.length();
for (int i = 0; i < tempIndex; i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
标签:return,String,timestamp,建模,接口,引擎,import,put,new
From: https://www.cnblogs.com/LiliDEtoy/p/18534736