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

herofour444 2013-04-26 04:43:16
读取本地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.
...全文
416 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
2013-04-27
  • 打赏
  • 举报
回复
谁知道 怎么把Find/Replace 对话框 调回 初始的样子 我现在的样子 是这样的
  • 打赏
  • 举报
回复
这错误是不能连接数据库,看服务是否开启,用户名密码是否输入正确,防火墙是否关闭
herofour444 2013-04-26
  • 打赏
  • 举报
回复

 
$$callid=-1374414754 $$thread=[http-8880-Processor21] $$host=112.247.132.254 $$userid= $$ts=2013-04-26 19:45:35 $$remotecall=[pfxx] $$debuglevel=ERROR  $$msg= get data source design error,can't connect to database
nc.jdbc.framework.exception.DB2Exception:  get data source design error,can't connect to database
	at nc.jdbc.framework.exception.ExceptionFactory.getException(ExceptionFactory.java:20)
	at nc.jdbc.framework.JdbcSession.<init>(JdbcSession.java:67)
	at nc.jdbc.framework.JdbcPersistenceManager.init(JdbcPersistenceManager.java:983)
	at nc.jdbc.framework.JdbcPersistenceManager.<init>(JdbcPersistenceManager.java:68)
	at nc.jdbc.framework.PersistenceManager.getInstance(PersistenceManager.java:440)
	at nc.bs.pub.SuperDMO.insertArray(SuperDMO.java:543)
	at nc.bs.pfxx.pub.PfxxDAO.writeLogs(PfxxDAO.java:34)
	at nc.bs.pfxx.pub.PFxxEJBServiceImpl.writeLogs_RequiresNew(PFxxEJBServiceImpl.java:45)
	at nc.itf.uap.pfxx.PfxxEJBEjbBean.writeLogs_RequiresNew(PfxxEJBEjbBean.java:68)
	at nc.itf.uap.pfxx.PfxxEJB_Local.writeLogs_RequiresNew(PfxxEJB_Local.java:25)
	at sun.reflect.GeneratedMethodAccessor183.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at nc.bs.framework.ejb.AbstractEJBServiceHandler.invoke(AbstractEJBServiceHandler.java:56)
herofour444 2013-04-26
  • 打赏
  • 举报
回复

openServer("http://221.2.160.163:8880/service/XChangeServlet?account=0001&receiver=0001");
 POST(); 
一个是连接,一个是发送,返回的错误是不完整。被搞崩溃了。
herofour444 2013-04-26
  • 打赏
  • 举报
回复
en,我也查了这个错误,是不完整造成的,但是不知道如何解决
  • 打赏
  • 举报
回复
Premature end of file. 这就是读取xml不完整造成的。
herofour444 2013-04-26
  • 打赏
  • 举报
回复

	System.out.println("------kaishi");
	openServer("http://221.2.160.163:8880/service/XChangeServlet?account=0001&receiver=0001");
	POST();
	//saveFile(getReceiverMessage());
注释掉有instream的后台还是那个错误
  • 打赏
  • 举报
回复
InputStream都换为new ByteArrayInputStream(FileUtils.readFileToByteArray(file)看看
herofour444 2013-04-26
  • 打赏
  • 举报
回复
这些xml手工在nc下面导入没有问题 fangmingshijie 你好,请问是哪个地方的第2次啊
  • 打赏
  • 举报
回复
第二次应该用ByteArrayInputStream读取了,还用InputStream读,不是从头开始了。
herofour444 2013-04-26
  • 打赏
  • 举报
回复
整理了一下代码,并且重新在eclipse 里面新建了一个xml,结果错误依旧
herofour444 2013-04-26
  • 打赏
  • 举报
回复

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,092

社区成员

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

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