//传入拼接好的xml字符串
public String addOrUpdateBill(String xml){
Logger.error("输入参数xml:" + xml);
String result = null;
String url = getUrl("datasource");//获取需要的参数
Logger.error("url:" + url);
if (url == null) {
Logger.error("获取url地址错误");
url = "http://127.0.0.1:99/service/XChangeServlet?account=develop&groupcode=01";//根据实际需求修改
}
URL realURL;
try {
realURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) realURL.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("content-type", "dept/xml");
connection.setRequestMethod("POST");
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
// 设定格式
XMLOutputter out = new XMLOutputter();
Logger.error("processing completed,ready to send data to url:"+ url);
out.output(new DOMBuilder().build(doc),connection.getOutputStream());
Logger.error("send success");
// 从连接的输入流中取得回执信息
InputStream inputStream = connection.getInputStream();
result = IOUtils.toString(inputStream, "UTF-8");
Logger.error("result:" + result);
}
result就是xml发送之后的回执信息,可以通过判断其标签的值来判断xml是否发送成功,如果为1则表示成功
String res = (result.split("<resultcode>")[1].split("<"))[0];
if(res.equals("1")){
//...成功之后的逻辑
}else{
//...失败之后的逻辑
}
//传入的参数code为需要的配置文件的对应的值
private String getUrl(String code) {
FileInputStream input = null;
String result = null;
Properties properties = new Properties();
//获取配置文件URL.properties的路径,HXBillImpl是和配置文件同级目录的实现类
String parth = HXBillImpl.class.getResource("").getPath();
Logger.error("HXBillImpl.parth:" + parth);
File fileB = new File(parth);
try {
String newparth = fileB + File.separator + "URL.properties";
Logger.error("newparth:" + newparth);
input = new FileInputStream(newparth);
properties.load(input);
result = properties.getProperty(code);
} catch (Exception e) {
e.printStackTrace();
Logger.error("error message:" + e.getMessage());
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
Logger.error("finally error message:" + e.getMessage());
Logger.error("result:" + result);
}
}
}
return result;
}
可以把url路径放在配置文件中,内容
url为NC客户端的手动加载界面的目标url地址