packagecom.xiaocongjisuan.module.example;importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.UnsupportedEncodingException;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjava.util.HashMap;importjava.util.Map;public classApplication {public static final String DEF_CHATSET = "UTF-8";public static final int DEF_CONN_TIMEOUT = 30000;public static final int DEF_READ_TIMEOUT = 30000;public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";//配置您申请的appKey和openId
public static final String APP_KEY ="yours";public static final String OPEN_ID ="yours";//将map型转为请求参数型
public static String urlEncode(Mapparams) {if(params==null){return "";};
StringBuilder sb= newStringBuilder();for (Map.Entryi : params.entrySet()) {try{
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String r=sb.toString();if(r.endsWith("&")){
r= r.substring(0,r.length()-1);
}returnr;
}/***
*@paramrequestUrl 请求地址
*@paramparams 请求参数
*@parammethod 请求方法
*@return请求结果
*@throwsException*/
public static String requestContent(String requestUrl, Map params,String method) throwsException {
HttpURLConnection conn= null;
BufferedReader reader= null;
String rs= null;try{//组装请求链接
StringBuffer sb = newStringBuffer();if(method!=null&&method.equalsIgnoreCase("get")){
requestUrl= requestUrl+"?"+urlEncode(params);
}//默认get
URL url = newURL(requestUrl);
conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");if(method!=null&&method.equalsIgnoreCase("post")){
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
}//参数配置
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();if (params!= null && method.equalsIgnoreCase("post")) {try{
DataOutputStream out= newDataOutputStream(conn.getOutputStream());
out.writeBytes(urlEncode(params));
}catch(Exception e) {
e.printStackTrace();
}
}//读取数据
InputStream is =conn.getInputStream();
reader= new BufferedReader(newInputStreamReader(is, DEF_CHATSET));
String strRead= null;while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs=sb.toString();
}catch(IOException e) {
e.printStackTrace();
}finally{if (reader != null) {
reader.close();
}if (conn != null) {
conn.disconnect();
}
}returnrs;
}public static void main(String[] args) throwsException{
String domain="http://api.xiaocongjisuan.com/";
String servlet="develop/officeexcelgenerate/get";
String method="get";
String requestUrl=domain+servlet;
Map params=new HashMap();
params.put("appKey",APP_KEY);
params.put("openId",OPEN_ID);//变动部分
params.put("toFormat","xlsx");
params.put("content","{\"sheets\":[{\"header\":[\"姓名\",\"性别\",\"年龄\"],\"rows\":[[\"张三\",\"男\",\"25\"],[\"李四\",\"男\",\"20\"],[\"小红\",\"女\",\"21\"]]},{\"header\":[\"姓名\",\"成绩\"],\"rows\":[[\"张三\",25],[\"李四\",20],[\"小红\",21]]}]}");
String result=requestContent(requestUrl,params,method);
System.out.println(result);
}
}