クライアントから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() );