求助发送到远程机器xml:Premature end of file错误

herofour444 2013-04-26 04:42:20
读取本地xml文件然后,发送到远程 NC系统中。
原始代码: 需要用到dom4j.jar

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
public class HttpSend {
public static URL url;
public static URLConnection conn;
public static void main(String[] args) throws IOException, SAXException, DocumentException
{
System.out.println("------kaishi");
openServer("http://221.2.160.163:8880/service/XChangeServlet?account=0001&receiver=0001");
POST();
saveFile(getReceiverMessage());
}

public static void POST() throws DocumentException {
sendMessage();
}
private static void sendMessage() throws DocumentException {
try {
OutputStream raw = conn.getOutputStream();
OutputStream buf = new BufferedOutputStream(raw);
OutputStreamWriter out = new OutputStreamWriter(buf);
// org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile, errors, entityResolver ).read( new File(xmlFile ) );
SAXReader saxReader = new SAXReader();
Document content = saxReader.read("c://uuu.xml");
content.setXMLEncoding("UTF-8");

// out.write(new String(content));
out.write(content.asXML());
out.flush();
out.close();
raw.close();
} catch (IOException e) {
new IOException("chuan song shi bai");
}
}

public static Document getReceiverMessage() {
try {

InputStream raw = conn.getInputStream();
InputStream in = new BufferedInputStream(raw);
Reader reader = new InputStreamReader(in);
BufferedReader bufreader = new BufferedReader(reader);

String xmlString = "";

int c;
System.out.println("==================Beging====================");
while ((c = bufreader.read()) != -1) {
System.out.print((char) c);
xmlString += (char) c;
}
in.close();
System.out.println("===================End======================");
Document document = DocumentHelper.parseText(xmlString);
return document;
} catch (DocumentException e) {
new DocumentException("huizhibuhefa");
} catch (IOException e) {
new IOException("wu fa huo qu hui zhi wen jian");
}
return null;
}

public static void openServer(String urlString) {
try {
checkHTTP(urlString);
conn = url.openConnection();
conn.setDoOutput(true);
System.out.println("---------------lianjiechenggong----------------");
} catch (IOException e) {
new IOException("lianjieshibai");
}
}

public static void saveFile(Document doc) {
Element root = doc.getRootElement();


String filename = "c://hello//hello.xml";
try {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(
new FileWriter(new File(filename)), format);

writer.write(doc);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
protected static void checkHTTP(String urlString) throws ProtocolException {
try {
url = new URL(urlString);
if (url == null || !url.getProtocol().toUpperCase().equals("HTTP"))
throw new ProtocolException("bushi http");
url = url;
} catch (MalformedURLException m) {
throw new ProtocolException("xie yi ge shi cuo wu");
}
}
}



本地XML文件:

<?xml version="1.0" encoding='UTF-8'?>
<ufinterface account="0001" billtype="bsmeasdoc" filename="" isexchange="Y" proc="add" receiver="K01" replace="Y" roottag="" sender="001" subbilltype="">
<bill id="">
<billhead>
<basecodeflag>0</basecodeflag>
<DR>0</DR>
<measname>aaaa</measname>
<oppdimen>长度</oppdimen>
<scalefactor>1.0</scalefactor>
<shortname>09</shortname>
</billhead>
</bill>
</ufinterface>



打印结果:
<resultcode>-31003</resultcode>
<resultdescription>从输入流转换document出错:请检验文档格式。\n</resultdescription>


后台提示:
[Fatal Error] :-1:-1: Premature end of file.
...全文
127 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
herofour444 2013-04-26
  • 打赏
  • 举报
回复
这些xml手工在nc下面导入没有问题
herofour444 2013-04-26
  • 打赏
  • 举报
回复
整理了一下代码,并且重新在eclipse 里面新建了一个xml,结果错误依旧

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
public class HttpSend {
 public static URL url;
 public static URLConnection conn;
public static void main(String[] args) throws IOException, SAXException, DocumentException
{
System.out.println("------kaishi");
openServer("http://221.2.160.163:8880/service/XChangeServlet?account=0001&receiver=0001");
POST();
saveFile(getReceiverMessage());
}

 public static void POST() throws DocumentException {
  sendMessage();
 }
 private static void sendMessage() throws DocumentException {
  try {
   OutputStream raw = conn.getOutputStream();
 OutputStream buf = new BufferedOutputStream(raw);
   OutputStreamWriter out = new OutputStreamWriter(buf);
//   org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile, errors, entityResolver ).read( new File(xmlFile ) );
    SAXReader saxReader = new SAXReader(); 
    Document content = saxReader.read("c://uuu.xml");
    content.setXMLEncoding("UTF-8");
    
   // out.write(new String(content));
   out.write(content.asXML());
   out.flush();
   out.close();
   raw.close();
  } catch (IOException e) {
   new IOException("chuan song shi bai");
  }
 }

 public static Document getReceiverMessage() {
  try {

   InputStream raw = conn.getInputStream();
   InputStream in = new BufferedInputStream(raw);
   Reader reader = new InputStreamReader(in);
   BufferedReader bufreader = new BufferedReader(reader);

   String xmlString = "";

   int c;
   System.out.println("==================Beging====================");
   while ((c = bufreader.read()) != -1) {
    System.out.print((char) c);
    xmlString += (char) c;
   }
   in.close();
   System.out.println("===================End======================");
   Document document = DocumentHelper.parseText(xmlString);
   return document;
  } catch (DocumentException e) {
   new DocumentException("huizhibuhefa");
  } catch (IOException e) {
   new IOException("wu fa huo qu hui zhi wen jian");
  }
  return null;
 }

 public static void openServer(String urlString) {
  try {
  checkHTTP(urlString);
   conn = url.openConnection();
   conn.setDoOutput(true);
   System.out.println("---------------lianjiechenggong----------------");
  } catch (IOException e) {
   new IOException("lianjieshibai");
  }
 }

 public static void saveFile(Document doc) {
  Element root = doc.getRootElement();


  String filename = "c://hello//hello.xml";
  try {
   OutputFormat format = OutputFormat.createPrettyPrint();
   format.setEncoding("UTF-8");
   XMLWriter writer = new XMLWriter(
     new FileWriter(new File(filename)), format);

   writer.write(doc);
   writer.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
 protected static void checkHTTP(String urlString) throws ProtocolException {
  try {
   url = new URL(urlString);
   if (url == null || !url.getProtocol().toUpperCase().equals("HTTP"))
    throw new ProtocolException("bushi http");
    url = url;
  } catch (MalformedURLException m) {
   throw new ProtocolException("xie yi ge shi cuo wu");
  }
 }
}

七神之光 2013-04-26
  • 打赏
  • 举报
回复

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧