http://blog.csdn.net/fhd001/article/details/5789406
服务接口:
package cxf.server;
import javax.jws.WebService;
@WebService
publicinterface SendPicture {
String sendPicture(Picture picture);
}
服务实现:
package cxf.server;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.jws.WebService;
@WebService(endpointInterface = "cxf.server.SendPicture")
publicclass SendPictureImpl implements SendPicture {
@Override
public String sendPicture(Picture picture) {
try {
DataHandler handler = picture.getImag();
InputStream is = handler.getInputStream();
OutputStream os = new FileOutputStream(new File("D://8.gif"));
byte[] b = newbyte[100000];
int bytesRead = 0;
while ((bytesRead = is.read(b)) != -1) {
os.write(b, 0, bytesRead);
}
os.flush();
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("server: " + "FHD");
return"YES";
}
}
传输的数据对象,注意DataHandler这个属性:
package cxf.server;
import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlMimeType;
@XmlAccessorType(XmlAccessType.FIELD)
publicclass Picture {
@XmlMimeType("application/octet-stream")
private DataHandler imag;
public DataHandler getImag() {
return imag;
}
publicvoid setImag(DataHandler imag) {
this.imag = imag;
}
}
服务端配置:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<importresource="classpath:META-INF/cxf/cxf.xml"/>
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpointid="SendPicture"implementor="cxf.server.SendPictureImpl"
address="/SendPicture">
<jaxws:properties>
<entrykey="mtom-enabled"value="true"/>
</jaxws:properties>
</jaxws:endpoint>
</beans>
客户端配置:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:clientid="client"
address="http://localhost:8085/java_first_spring_support1/service/SendPicture"
serviceClass="cxf.server.SendPicture"/>
</beans>
客户端调用:
package cxf.client;
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cxf.server.Picture;
import cxf.server.SendPicture;
publicfinalclass Client {
publicstaticvoid main(String args[]) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
SendPicture client = (SendPicture)context.getBean("client");
Picture picture = new Picture();
DataSource source = new FileDataSource(new File("F://3.gif"));
picture.setImag(new DataHandler(source));
String str = client.sendPicture(picture);
System.out.println("client: " + str);
System.exit(0);
}
}
请注意:
@XmlMimeType("application/octet-stream")
private DataHandler imag;
还有:
<jaxws:properties>
<entry key="mtom-enabled" value="true" />
</jaxws:properties>