方法二
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ProjectPathUtils { public static String getProjectPath(String type){ InputStream in = ProjectPathUtils.class.getResourceAsStream("/config/properties/projectPath.properties"); Properties prop = new Properties(); try { prop.load(in); String property = prop.getProperty(type); return property; } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return type; } }propertis数据
getItem=http://192.168.1.151/item/getItem.action
httpUtil
/** * * @param url : 请求的url地址 * @param parameterMap: 请求参数 * @return */ public static String sendRequest(String url, Map<String,Object> parameterMap) throws IOException { String msg = null; HttpPost httpPost = new HttpPost(url); HttpEntity reqEntity = null; CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; if(parameterMap!=null && parameterMap.size()>0){ MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Map.Entry<String,Object> entry:parameterMap.entrySet()) { Class clazz = entry.getValue().getClass(); if(clazz == String.class){ StringBody param = new StringBody( entry.getValue().toString(),ContentType.create("text/plain", Consts.UTF_8)); builder.addPart(entry.getKey(),param); } } reqEntity = builder.build(); httpPost.setEntity(reqEntity); } response = httpClient.execute(httpPost); try { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { msg = EntityUtils.toString(resEntity, Charset.forName("UTF-8")); } EntityUtils.consume(resEntity); } finally { response.close(); httpClient.close(); } return msg; }调用
@RequestMapping(value="/item/getItem",method=RequestMethod.POST,produces = "application/json; charset=utf-8") String projectPath = ProjectPathUtils.getProjectPath("getItem"); String requestResult = HttpUtil.sendRequest(projectPath, null); return requestResult;