java - 无法将 Document 转换为 MockMultipartFile
问题描述
我需要将 a 转换Document
为 a MultipartFile
,并且我发现我可以使用MockMultipartFile
. 问题是我无法获得InputStream
MockMultipartFile 所需的内容。我正在做的是这个,我在另一个问题中发现:
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);
但是有一个问题xmlWriter.write(doc);
,因为抛出异常说:
Invalid object: [#document: null]
我看过这个答案,但我的 XML 不是计算机上的文件,它是我从数据库中读取的字符串,我转换为 XML。
我也试过这个:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputFormat outputFormat = new OutputFormat(document);
XMLSerializer serializer = new XMLSerializer(outputStream, outputFormat);
serializer.serialize(document);
return new ByteArrayInputStream(outputStream.toByteArray());
但new OutputFormat
已弃用。
解决方案
推荐阅读
- windows - 手动将exe添加到控制面板中的程序和功能
- python - 这个错误是什么意思?Python 数组列表/队列
- android - 如何在 Flutter 中检索没有文本字段的文本输入
- html - 如何订阅两个输入字段并使它们在 Angular 2 中相互切换?
- python - 按 ID 展平数据
- javascript - 在数组中映射嵌套数据对象
- python - TensorFlow 绿移图像
- android - Flutter - 在 Firebase 消息传递中出现错误
- javascript - KTDatatable 不会将数据导出为 pdf、excel、csv 文件
- anylogic - Anylogic端口交替使用(?)