对于某些从泛型(比如:Map<K, V>)中继承过来的数据,K可能是String、Integer、等等。如果需要map.get(key),得先确保key的类型跟map的K匹配。
对于key类型和value类型的判断,大概的思路:通过paramsMap.entrySet()获取集合的key和value值,存在变量中;然后有两种方法判断它的类型:
1、instanceof
可以判断基础属性和对象属性
public CloseableHttpResponse post(String url, Map<String, Object> paramsMap, HashMap<String,String> headerMap) throws ClientProtocolException, IOException {
//创建一个Httppost的请求对象
HttpPost httppost = new HttpPost(url);
Object valueClass =null;
//获取paramsMap中的value的class,用于判断value的类型
for (Map.Entry<String, Object> arg : paramsMap.entrySet()) {
valueClass = arg.getValue();
break;
}
String key = null;
File value = null;
//用value的类型和File类型比较,如果类型是File执行上传文件的请求,如果去其他类型则普通表单的请求
if (valueClass instanceof File) {
//取出paramsMap里的key和value
for (Map.Entry<String, Object> arg : paramsMap.entrySet()) {
key = arg.getKey();
value = (File) arg.getValue();
break;
}
FileBody bin = new FileBody(value, ContentType.create("image/png", Consts.UTF_8));//创建图片提交主体信息
HttpEntity entity = MultipartEntityBuilder
.create()
.setCharset(Charset.forName("utf-8"))
.addPart(key, bin)//添加到entity里
.build();
httppost.setEntity(entity);
System.out.println("执行到了上传图片的请求");
} else {
List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
//迭代Map-->取出key,value放到BasicNameValuePair对象中-->添加到list中
for (String key1 : paramsMap.keySet()){
pairList.add(new BasicNameValuePair(key1,paramsMap.get(key1).toString()));
}
UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairList,"utf-8");
httppost.setEntity(uefe);
System.out.println("执行到了普通表单的请求");
}
//创建一个可关闭的HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//加载请求头到httppost对象
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
httppost.addHeader(entry.getKey(), entry.getValue());
}
//发送post请求
CloseableHttpResponse httpResponse = httpclient.execute(httppost);
return httpResponse;
}
测试方法:
@Test
public void uploadFileTest1()throws IOException{
SqlSession sqlSession = DatabaseUtil.getSqlSession("databaseConfig.xml");
SelectByPageCase selectByPageCase = sqlSession.selectOne("selectByPage",1);
HashMap<String,String> headermap = new HashMap<>();
headermap.put("Authorization",selectByPageCase.getAuthorization());
Log.info("获取到的token=" + selectByPageCase.getAuthorization());
//获取电脑上的图片
File file = new File("D:\\截图.png");
Map<String,Object> parammap = new HashMap<>();
parammap.put("file",file);
closeableHttpResponse = restClient.post(url,parammap,headermap);
JSONObject responseJson = restClient.getResponseJson(closeableHttpResponse);
Log.info("响应内容:" + responseJson);
int code = JsonPath.read(responseJson,"$.code");
Assert.assertEquals(code,selectByPageCase.getJpath(),"用例执行失败");
}
测试结果:
2、class 的 equals()方法
只能判断基础属性
//获取cache中的key的class,用于判断key的类型
Class<? extends Object> keyClass = null;
Map<Object, Object> m = cache.getCache().asMap();
Set<Object> s = m.keySet();
for(Object k : s){
keyClass = k.getClass();
break;//只需要判断第一个元素
}
//根据cache中key的类型做类型转换:
if(keyClass.equals(Integer.class)){ //Integer类型
List<Integer> iKeys = new LinkedList<>();
for(Object key : keys){
iKeys.add(Integer.valueOf((String) key));
}
cache.getCache().invalidateAll(iKeys);
}else { //缺省为String类型,不用转换
cache.getCache().invalidateAll(keys);
}