public static String getJsonData(JSONObject jsonParam) {
StringBuffer sb=new StringBuffer();
String resultJsonto = new String();
try {
// 創建url資源
URL url = new URL("http://XXXXXXXXX");
// 建立http連接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 設置允許輸出
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(100000);
// 設置允許輸入
conn.setDoInput(true);
// 設置不用緩存
conn.setUseCaches(false);
// 設置傳遞方式
conn.setRequestMethod("POST");
// 設置維持長連接
conn.setRequestProperty("Connection", "Keep-Alive");
// 設置文件字符集:
conn.setRequestProperty("Charset", "UTF-8");
// 轉換為字節數組
//byte[] data = (jsonParam.toString()).getBytes();
byte[] data = jsonParam.toString().getBytes();
// 設置文件長度
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
// 設置文件類型:
conn.setRequestProperty("contentType", "application/json");
// 開始連接請求
conn.connect();
OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
// 寫入請求的字符串
out.write(jsonParam.toString().getBytes());
out.flush();
out.close();
System.out.println(conn.getResponseCode());
// 請求返回的狀態
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()){
System.out.println("連接成功");
// 請求返回的數據
InputStream in1 = conn.getInputStream();
try {
String readLine=new String();
BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
while((readLine=responseReader.readLine())!=null){
sb.append(readLine).append("\n");
}
responseReader.close();
String json = sb.toString();
//第一次解碼
String deCode = deCodeBase(json);
//分割字符串
String nextIndex =deCode.substring(deCode.indexOf("{"), deCode.length());
//轉換json格式
JSONObject jsonResult =JSONObject.fromObject(nextIndex);
//獲取解碼message值
String deCodename = deCodeBase(jsonResult.getString("message"));
//獲取result值
String resultname = jsonResult.getString("result");
//返回新JSONObject
JSONObject resultJson = new JSONObject();
resultJson.put("message", deCodename);
resultJson.put("result", resultname);
//實體類對象轉換成String類型的JSON字符串
resultJsonto = resultJson.toString();
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
System.out.println("連接失敗");
InputStream error = conn.getErrorStream();
String errorLine=new String();
BufferedReader errorresponseReader=new BufferedReader(new InputStreamReader(error,"UTF-8"));
while((errorLine=errorresponseReader.readLine())!=null){
sb.append(errorLine).append("\n");
}
errorresponseReader.close();
String errorData = sb.toString();
System.err.println("錯誤信息:"+errorData);
}
} catch (Exception e) {
System.out.println("error");
e.printStackTrace();
}
return resultJsonto;
}
調用接口
public Map<Integer,Object> work(String str1,String str2,String file) throws Exception {
str1 = "123";
str2 = "456";
String mima = UUID.randomUUID().toString();
//密鑰加密
String requestData = enCodeBase(str1);
//密鑰加密
String sign = DigestUtils.md5DigestAsHex(str2.getBytes());
//讀取文本文件
String name = readFileContent(file);
//獲取文本文件內參數
String utf8 = "";
try {
utf8 = new String(name.getBytes(), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.err.println("utf8:"+utf8);
//切割字符串,獲取參數
String[] rows = utf8.split("<br>");
//批量次數
Integer rowNum = rows.length;
//獲取返回值
Map<Integer,Object> resultStr = new HashMap<>();
for (int i = 0;i<rows.length;i++) {
System.err.println("Stirng:"+rows[i]);
//Stirng 轉為jsonJSONObject
JSONObject string1 = JSONObject.fromObject(rows[i]);
//調用接口
String str=UserController.getJsonData(string1);
//防止結果到map
resultStr.put(i, str);
}
for(Entry<Integer, Object> entry : resultStr.entrySet()){
Integer mapKey = entry.getKey()+1;
Object mapValue = entry.getValue();
System.out.println("批次:"+mapKey+" : 錯誤信息:"+mapValue);
}
return resultStr;
}
相關加密,導入文件方法
//base64位的加密狀態
public static String enCodeBase(String str){
byte[] enCodeBase;
try {
enCodeBase = Base64.encodeBase64(str.getBytes("utf-8"));
String enCode = new String(enCodeBase,"UTF-8");
return enCode;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "出現異常";
}
}
//base64位的解密
public static String deCodeBase(String str){
byte[] deCodeBase64;
try {
deCodeBase64 = Base64.decodeBase64(str.getBytes("UTF-8"));
String deCode = new String(deCodeBase64,"UTF-8");
return deCode;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "出現異常";
}
}
//獲取文本文件
public static String readFileContent(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return sbf.toString();
}
