//通过@Test或者main方法来进行 相关依赖
//poi
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> </dependency>
//httpClient
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
@Test
public void testConfig() {
try {
//读取信息xlsx文件 该文件中有批量操作数据
InputStream in = new FileInputStream("C:\\Users\\\\Desktop\\domain.xlsx");
//利用poi进行xlsx文件简析
Workbook wb = WorkbookFactory.create(in);
//获取sheet
Sheet sheet = wb.getSheetAt(0);
//获取第一行行数
int rmin = sheet.getFirstRowNum();
//获取最后一行行数
int rmax = sheet.getLastRowNum();
//构建map的set集合 去重处理
Set<Map<String, String>> params = Sets.newHashSet();
//去除第一行(标题行)
for (int i = rmin + 1; i <= rmax; i++) {
//获取每行内容
Row row = sheet.getRow(i);
//获取当前行中第一个单元格内容
Cell cell_0 = row.getCell(0);
Map<String, String> map = Maps.newHashMap();
map.put("id", cell_0.getStringCellValue());
//获取当前行中第二个单元格内容
Cell cell_1 = row.getCell(1);
map.put("name", cell_1.getStringCellValue());
params.add(map);
}
int i = 0;
for (Map<String, String> param : params) {
Map<String, Object> jsonObject = new HashMap<>();
jsonObject.put("id", param.get("id"));
jsonObject.put("name", param.get("name"));
i++;
System.out.println(i);
//访问的url
// String post = post("url", jsonObject);
// System.out.println("发起请求结束,操作结果={}" + post);
}
}catch (Exception e){
System.out.println(e);
}
}
//post方法
public static String post(String url, Map<String, Object> mapParam){
//创建httpClient对象
HttpClient client = new DefaultHttpClient();
//创建HttpPost 对象
HttpPost post = new HttpPost(url);
//添加cookie信息 根据需求
//post.addHeader(new BasicHeader("Cookie","111; JSESSIONID=222.s1"));
String body = null;
try {
//设置StringEntity对象,将map转换为json
StringEntity s = new StringEntity(JsonUtils.toJSONString(mapParam), "UTF-8");
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
//执行post请求,返回响应体HttpResponse
HttpResponse res = client.execute(post);
//查看响应码 200成功
if(res.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
//对响应数据进行处理
HttpEntity entity = res.getEntity();
body = entity != null ? EntityUtils.toString(entity) : null;
System.out.println(body);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return body;
}