webService客户端参数为DataHandler类型时候的参数组装
一般我们webservice的方法参数或返回值为byte类型的时候,在生成的客户端代码里的参数却是DataHandler,为此我纠结了好久。网上查的全是什么图片上传,其实我就想传递一个简单的字符串的字节码,跟什么文件上传屁兜不打岔。最后我弄出来了。上代码,以便以后使用。 package com.imp; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; import javax.activation.DataHandler; import javax.activation.DataSource; public class TestMain { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub HelloDaoImpStub stub =new HelloDaoImpStub(); Say say =new Say(); String s="23323232"; byte[] b= s.getBytes(); say.setB(createDataHandler(b)); ? SayResponse response = stub.say(say); DataHandler result = response.get_return();
System.out.println(result.getName()); }
public static DataHandler createDataHandler(final byte[] b) { return new DataHandler(new DataSource() { ? ? ? ? ? ?public InputStream getInputStream() { ? ? ? ? ? ? ? ? ?return new ByteArrayInputStream(b); ? ? ? ? ? ? ?} ? ? ? ? ? ? ?public OutputStream getOutputStream() { ? ? ? ? ? ? ? ? ?return null; ? ? ? ? ? ? ?} ? ? ? ? ? ? ?public String getContentType() { ? ? ? ? ? ? ? ? ?return ""; ? ? ? ? ? ? ?} ? ? ? ? ? ? ?public String getName() { ? ? ? ? ? ? ? ? ?return ""; ? ? ? ? ? ? ?} }); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |