我有这个服务器必须从客户端接收xml文件。一切正常,我的服务器创建一个新的xml文件。之后我尝试使用JAXB从XML文件中读取,但是我得到了SAXParseException。我已尝试使用多个文件,而在另一个类中它可以正常工作,但不在Server类
中 private void saveFile(Socket clientSock) throws IOException, JAXBException {
DataInputStream dis = new DataInputStream(clientSock.getInputStream());
FileOutputStream fos = new FileOutputStream("testfile.xml");
byte[] buffer = new byte[4096];
int filesize = 15123; // Send file size in separate msg
int read = 0;
int totalRead = 0;
int remaining = filesize;
while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
fos.close();
dis.close();
JAXBContext jac;
jac = JAXBContext.newInstance(Product.class,Order.class,Orders.class,Products.class);
Marshaller jaxbMarshaller = jac.createMarshaller();
jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
Unmarshaller jaxbUnmarshaller = jac.createUnmarshaller();
**Orders newOrders = (Orders)jaxbUnmarshaller.unmarshal( new File("testfile.xml"));**
System.out.println( newOrders.getOrders().get(1).getId() );