// 获取Servlet连接并设置请求的方法
String url = "http://172.16.99.31:8003/service/XChangeServlet?account=0003&receiver=80000002";
URL realURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection)realURL.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "text/xml");
connection.setRequestMethod("POST");
File file = new File("E:\\用友\\数据文件目录\\会计凭证.xml");
BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
int length;
byte[] buffer = new byte[1000];
while ((length = input.read(buffer, 0, 1000)) != -1) {
out.write(buffer, 0, length);
}
input.close();
out.close();
// 从连接的输入流中取得回执信息
/***************从输入流取得Doc***************/
InputStream inputStream = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader bufreader = new BufferedReader(isr);
String xmlString = "";
int c;
System.out.println("==================Beging====================");
while ((c = bufreader.read()) != -1) {
System.out.print((char) c);
xmlString += (char) c;
}
input.close();
System.out.println("===================End======================");
Document resDoc = DocumentHelper.parseText(xmlString);
// 对回执结果的后续处理
/************document转化为xml*************/
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DocumentSource source = new DocumentSource(resDoc);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//设置文档的换行与缩进
transformer.setOutputProperty(OutputKeys.INDENT, "YES");
//设置日期格式
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
String resFile = "E:\\用友\\回执目录\\BkMsg_会计凭证_"+fmt.format(new Date())+".xml";
StreamResult result = new StreamResult(new File(resFile));
transformer.transform(source,result);
System.out.println("======生成回执文件成功=======");
/**************jdom解析XML*****************/
org.jdom.input.SAXBuilder saxReader = new SAXBuilder();
org.jdom.Document document1 = saxReader.build(new File(resFile));
org.jdom.Element root = document1.getRootElement();
//获取根元素,得到导入用友是否成功successful的值,值为Y:成功 N:失败
String resSuc = root.getAttributeValue("successful");
List<org.jdom.Element> list = root.getChildren();
for(org.jdom.Element e:list){
System.out.println("-------------------------");
System.out.println("filename---> "+e.getChildText("filename"));
System.out.println("resultcode---> "+e.getChildText("resultcode"));
System.out.println("resultdescription---> "+e.getChildText("resultdescription"));
System.out.println("content--> "+e.getChildText("content"));
System.out.println("--------------------------------------");
}
//后面对回执结果做判断,然后改变导入状态就行了
if(null != resSuc){
if(resSuc.equals("N")){
System.out.println("导入失败");
}else if(resSuc.equals("Y")){
System.out.println("导入成功");
//接下来的代码,修改状态
}else{
System.out.println("出现未知错误");
}
}else{
System.out.println("未找到successful属性");
}