webservice要求我设置一个应该是xml附件的DataHandler类型。
DataSource dataSource = new FileDataSource(tempFile.getAbsolutePath());
DataHandler dataHandler = new DataHandler(dataSource);
request.setDataHandler(dataHandler);问题在于,从Axis2生成的SOAPMessage的值为base64
<dataHandler>big string64 string representing my content</dataHandler>它应该在哪里呢?
<dataHandler><inc:Include href="cid:attachmentid" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></dataHandler>
Content-Type: text/xml; charset=us-ascii; name=Sample.xml
Content-Transfer-Encoding: 7bit
Content-ID: <attachmentid>
Content-Disposition: attachment; name="Sample.xml"; filename="Sample.xml"
... the xml content....WSDL
<xsd:element name="dataHandler" type="xsd:base64Binary" maxOccurs="1" minOccurs="1" xmime:expectedContentTypes="application/octet-stream"/>我能做些什么来解决这个问题?
发布于 2019-09-27 18:09:21
我在我的xmlobject中有一个值,它是一个DataHandler,这是我的解决方案,将数据处理程序作为一个对象放入值中,其中包含xml:
带有根元素的XmlObject:
RootXmlObject xml = new RootXmlObject();然后设置marshaller将其转换为字符串:
JAXBContext context = JAXBContext.newInstance(RootXmlObject.class);
Marshaller mar= context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
mar.marshal(xml, sw);
String xmlString = sw.toString();创建自定义数据处理程序:
DataHandler.setDataContentHandlerFactory(new YourDatahandler());
private class YourDatahandler implements DataContentHandlerFactory {
@Override
public DataContentHandler createDataContentHandler(String mimeType) {
return new XmlDataContentHandler();
}
}
public static class XmlDataContentHandler implements DataContentHandler {
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {DataFlavor.stringFlavor};
}
@Override
public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException, IOException {
return new String("Whateverstring");
}
@Override
public Object getContent(DataSource dataSource) throws IOException {
return new String("Whateverstring");哪个"writeTo“方法是这样工作的:
@Override
public void writeTo(Object o, String s, OutputStream outputStream) throws IOException {
byte[] stringByte = (byte[]) ((String) o).getBytes("UTF-8");
outputStream.write(stringByte);
}最后,将xmlString插入到数据处理程序:
DataHandler testHandler = new DataHandler(xmlString, "text/xml");https://stackoverflow.com/questions/53764692
复制相似问题