When tring to upload a XML file to AWS S3, I found that it’s not allowed to create a tmp file in server local, so I had to transfer the file as InputStream
at first and upload it later.
This is the code of transfering the XML Document instance to InputStream
and Byte array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static InputStream documentToInputStream(Document doc) throws IOException { byte[] bytes = documentToBytes(doc); return new ByteArrayInputStream(bytes); } public static byte[] documentToBytes(Document doc) throws IOException { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { XMLWriter xmlWriter = new XMLWriter(outputStream, OutputFormat.createCompactFormat()); xmlWriter.write(doc); xmlWriter.close(); return outputStream.toByteArray(); } } |
As dom4j is been used to generate the XML, some classes from which (e.g. Document
, XMLWriter
) can be found in the code.