CXF之MTOM(传送二进制数据文件)实例 收藏
服务接口:
view plaincopy to clipboardprint?
package cxf.server;
import javax.jws.WebService;
@WebService
public interface SendPicture {
String sendPicture(Picture picture);
}
服务实现:
view plaincopy to clipboardprint?
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")
public class 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 = new byte[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这个属性:
view plaincopy to clipboardprint?
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)
public class Picture {
@XmlMimeType("application/octet-stream")
private DataHandler imag;
public DataHandler getImag() {
return imag;
}
public void setImag(DataHandler imag) {
this.imag = imag;
}
}
服务端配置:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="SendPicture" implementor="cxf.server.SendPictureImpl"
address="/SendPicture">
<jaxws:properties>
<entry key="mtom-enabled" value="true" />
</jaxws:properties>
</jaxws:endpoint>
</beans>
客户端配置:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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:client id="client"
address="http://localhost:8085/java_first_spring_support1/service/SendPicture"
serviceClass="cxf.server.SendPicture"/>
</beans>
客户端调用:
view plaincopy to clipboardprint?
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;
public final class Client {
public static void 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>