|
|
package com.da.dangan.util;
|
|
|
|
|
|
import cn.hutool.core.util.CharsetUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.http.Method;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.net.HttpCookie;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 调用第三方接口,传文件,获取识别结果
|
|
|
*/
|
|
|
public class CallThirdInterface {
|
|
|
|
|
|
public static String callThirdInterface1(String url){
|
|
|
// 定义上传的URL
|
|
|
//String url = "http://yourserver.com:8080/upload";
|
|
|
//添加json
|
|
|
/*JSONObject json = new JSONObject();
|
|
|
json.put("username", "1332788xxxxxx");
|
|
|
json.put("password", "123456.");*/
|
|
|
//添加请求头,可多个
|
|
|
HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
|
|
|
headers.put("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjA2NjhmYTM5LTE5OWEtNDAyYy1iYjJlLTk1MjI0OWExMTlhOSJ9.S5-6MC6xKHx1bGjuE0g_QR0vGTsAG1E6rmQCqm2GSMIgNNZkYCnZckegYI8UCCxuGkTV_GOFwmcGagG0IccNEw");
|
|
|
// 定义要上传的文件
|
|
|
// File file = new File("/path/to/your/file.txt");
|
|
|
|
|
|
// 定义其他要上传的表单参数
|
|
|
Map<String, Object> map = new HashMap<>();//存放参数
|
|
|
map.put("keyword", "王");
|
|
|
map.put("ywTypes", 0);
|
|
|
// 使用Hutool的HttpUtil上传文件
|
|
|
|
|
|
String result = HttpUtil.createGet(url)
|
|
|
.addHeaders(headers)
|
|
|
.charset(CharsetUtil.CHARSET_UTF_8)
|
|
|
//.form("file", file) // 文件名称为"file"
|
|
|
.form(map)// 添加其他表单参数
|
|
|
.execute().body() ;
|
|
|
// 输出结果
|
|
|
System.out.println(result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public static String callThirdInterface(String url,List<File> files,String fileType){
|
|
|
// 1. 创建HttpRequest对象 - 指定好 url 地址
|
|
|
HttpRequest httpRequest = new HttpRequest(url);
|
|
|
// 2. 设置请求方式,默认是GET请求
|
|
|
httpRequest.setMethod(Method.POST);
|
|
|
|
|
|
// 3. 设置请求参数 可通过 form表单方法 设置 可以是文件类型
|
|
|
// form方法有很多重载方法,可以一个一个参数设置,也可以将参数封装进一个map集合然后一块儿
|
|
|
// File file = new File("C:\\Users\\hssym\\Downloads\\UBQ.png");
|
|
|
|
|
|
httpRequest.charset(CharsetUtil.CHARSET_UTF_8);
|
|
|
httpRequest.form("files[]",files.toArray(new File[files.size()]));
|
|
|
httpRequest.form("fileType",fileType);
|
|
|
//httpRequest.form("singeOrDouble",);
|
|
|
//httpRequest.form("picIds",ids.toArray(new Long[ids.size()]));
|
|
|
|
|
|
// 4. 设置请求头
|
|
|
// 请求头同样可以逐一设置,也可以封装到map中再统一设置
|
|
|
// 设置的请求头,可添加多个
|
|
|
// httpRequest.header("x-c-authorization","yourToken");
|
|
|
// httpRequest.header("Content-Type", "multipart/form-data;charset=UTF-8");
|
|
|
// 5. 执行请求,得到http响应类
|
|
|
HttpResponse execute = httpRequest.execute();
|
|
|
|
|
|
// 6. 解析这个http响应类,可以获取到响应主体、cookie、是否请求成功等信息
|
|
|
boolean ok = execute.isOk(); // 是否请求成功 判断依据为:状态码范围在200~299内
|
|
|
System.out.println(ok);
|
|
|
List<HttpCookie> cookies = execute.getCookies();// 获取所有cookie
|
|
|
cookies.forEach(System.out::println); // 如果为空不会遍历的
|
|
|
String body = execute.body(); // 获取响应主体
|
|
|
/*byte[] bytes = execute.bodyBytes();
|
|
|
String body = new String (bytes,CharsetUtil.UTF_8);*/
|
|
|
|
|
|
// 输出结果
|
|
|
System.out.println(body);
|
|
|
return body;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成添加各个乡镇的村和社区的语句
|
|
|
* @param args
|
|
|
*/
|
|
|
public static void main(String[] args) {
|
|
|
String num = "后屯村、陈家口村、北麦洼村、张家屯村、赵家屯村、潘家村、骆村、大屯村、祁刘村、丁家庵村、半壁店村、西高家村、北土路口村、琅窝村、孤城村、耿家庄村、南麦洼村、北护驾庄村、南护驾庄村、北尚家庄村、辛兴庄村、种家湾村";
|
|
|
String[] yujus = num.split("、");
|
|
|
StringBuffer values = new StringBuffer("INSERT INTO `dangan`.`area_sort`( `mu_name`, `area_type`, `create_by`, `create_time`,pid) VALUES");
|
|
|
for (String s : yujus) {
|
|
|
String y ="('"+s+"','2','admin', '2024-05-13 15:12:07',14),";
|
|
|
values.append(y);
|
|
|
}
|
|
|
System.out.println(values);
|
|
|
}
|
|
|
}
|