I need to convert a Document
to a MultipartFile
, and I've found I can use MockMultipartFile
. The problem is that I can't get the InputStream
that I need for my MockMultipartFile. What I'm doing is this, that I've found in another question:
Document doc = XmlUtilities.stringToXml(xml);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XMLWriter xmlWriter;
try {
xmlWriter = new XMLWriter(outputStream, OutputFormat.createPrettyPrint());
xmlWriter.write(doc);
xmlWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
MultipartFile mpfile = new MockMultipartFile(functionalityName, functionalityName, "text/xml", inputStream);
But there is a problem in xmlWriter.write(doc);
, because the exeption is thrown saying:
Invalid object: [#document: null]
I've had a look at this answer, but my XML is not a File on the computer, it's a String that I read from DB that I convert to XML.
I've also tried this:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputFormat outputFormat = new OutputFormat(document);
XMLSerializer serializer = new XMLSerializer(outputStream, outputFormat);
serializer.serialize(document);
return new ByteArrayInputStream(outputStream.toByteArray());
But new OutputFormat
is deprecated.