SystemSetting.json
[
{
"ADTD_INTERFACE_ADDRESS": "http://17.2.6.1:8080/LightningDataInterface/service/adtd",
"UPLOAD_PATH":"H:\\newworkspace\\flzhgl\\",
"WARNING_INTERFACE":"",
"USERNAME":"",
"PASSWORD":""
}
]
SystemSetting.java
package cn.edu.cqu.flzhgl.json.ldjc;
import java.io.UnsupportedEncodingException;
import com.google.gson.Gson;
import cn.edu.cqu.flzhgl.tools.ldjc.FileTools;
/**
* 系统配置信息
*/
public class SystemSetting {
private String ADTD_INTERFACE_ADDRESS;
/**
* 获取雷电数据接口地址
* @return
*/
public String get_ADTD_INTERFACE_ADDRESS()
{
try //获取系统设置
{
String aString2=new FileTools().ReadFileAsString((new FileTools().GetProjectRootPath()+"resources"+System.getProperty("file.separator")+"SystemSetting.json"));
SystemSetting foo[] = new Gson().fromJson(aString2, SystemSetting[].class);//将json文本信息转换为对象
ADTD_INTERFACE_ADDRESS=foo[0].ADTD_INTERFACE_ADDRESS;
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
//e.printStackTrace();
}
return ADTD_INTERFACE_ADDRESS;
}
}
Service.java获取该地址
package cn.edu.cqu.flzhgl.service.ldjc;
import java.io.IOException;
import org.springframework.stereotype.Service;
import cn.edu.cqu.flzhgl.domain.ldjc.WarningData;
import cn.edu.cqu.flzhgl.json.ldjc.SystemSetting;
import cn.edu.cqu.flzhgl.service.htgl.BaseService;
import cn.edu.cqu.flzhgl.tools.ldjc.HttpConnectUtil;
@Service
public class WarningDataService extends BaseService{
String separator = System.getProperty("file.separator");// 文件路径间隔符,为了windows和linux兼容
String BasePath;
String BaseHttpUrl = null;
// 获取业务网预警信息数据接口地址
private String GetBaseHttpUrl() {
if (BaseHttpUrl == null) {
BaseHttpUrl = new SystemSetting().get_WARNINGINTERFACE_PATH();
}
return BaseHttpUrl;
}
/**
* hq 连接测试
* @param AreaIndex
* @param Date
* @return
* @throws IOException
*/
public String test()throws IOException {
String url=GetBaseHttpUrl();
String returnstring=HttpConnectUtil.getHttp(url);//通过http请求返回字符串
returnstring = new String(returnstring.getBytes(),"utf-8");//转换字符编码
return returnstring;
}
}
HttpConnectUtil.java
package cn.edu.cqu.flzhgl.tools.ldjc;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import cn.edu.cqu.flzhgl.json.ldjc.SystemSetting;
public class HttpConnectUtil {
/**
* get方式
* @param url
* @author www.yoodb.com
* @return
*/
public static String getHttp(String url) {
String responseMsg = "";
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
try {
httpClient.executeMethod(getMethod);
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = getMethod.getResponseBodyAsStream();
int len = 0;
byte[] buf = new byte[1024];
while((len=in.read(buf))!=-1){
out.write(buf, 0, len);
}
responseMsg = out.toString("UTF-8");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放连接
getMethod.releaseConnection();
}
return responseMsg;
}
/**
* 程序中访问http数据接口,该方法为雷电数据接口专用
* 其它访问http数据接口可以使用上方的方法getHttp
*/
public static String getURLContent(String urlStr) {
/** 网络的url地址 */
URL url = null;
/** http连接 */
HttpURLConnection httpConn = null;
/**//** 输入流 */
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL(urlStr);
in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String str = null;
while ((str = in.readLine()) != null) {
sb.append(str);
}
} catch (Exception ex) {
System.out.println("请检查webapp/resources/SystemSetting配置文件的ADTD_INTERFACE_ADDRESS,当前为:"+new SystemSetting().get_ADTD_INTERFACE_ADDRESS());
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
}
String result = sb.toString();
// System.out.println(result);
return result;
}
}