xfire webservice传输超大数据的方式有两种:
1、使用DataHandler接口。
2、直接使用byte数组。
第一种方式:
package com.wxl.app.datahandler;
import javax.activation.DataHandler;
public interface DataHandlerService {
public DataHandler download();
public void upload(DataHandler dataHandler);
}
package com.wxl.app.datahandler;
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
public class DataHandlerServiceImpl implements DataHandlerService {
public DataHandler download() {
final File file = new File("c:/看见-柴静(精校版).pdf");
DataHandler dataHandler = new DataHandler(new FileDataSource(file));
return dataHandler;
}
public void upload(DataHandler dataHandler) {
}
}
package com.wxl.app.datahandler;
import java.io.File;
import java.io.IOException;
import javax.activation.DataHandler;
import org.apache.commons.io.FileUtils;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.soap.SoapConstants;
import org.codehaus.xfire.transport.http.HttpTransport;
public class DataHandlerClient {
public static void main(String[] args) throws IOException {
Service serviceModel = new ObjectServiceFactory().create(
DataHandlerService.class, null,
"http://test/DataHandlerService", null);
DataHandlerService service = (DataHandlerService) new XFireProxyFactory()
.create(serviceModel,
"http://localhost:8080/book/services/DataHandlerService");
Client client = Client.getInstance(service);
client.setProperty(SoapConstants.MTOM_ENABLED, "true");
client.setProperty(HttpTransport.CHUNKING_ENABLED, "true");
DataHandler handler = service.download();
FileUtils.copyInputStreamToFile(handler.getInputStream(), new File(
"d:/a.pdf"));
}
}
<service>
<name>DataHandlerService</name>
<namespace>http://test/DataHandlerService</namespace>
<serviceClass>com.wxl.app.datahandler.DataHandlerService</serviceClass>
<implementationClass>com.wxl.app.datahandler.DataHandlerServiceImpl</implementationClass>
<scope>request</scope>
<properties>
<property key="mtom-enabled">true</property>
</properties>
</service>
第二种方式:
package com.wxl.app.file;
import java.io.Serializable;
public class FileBean implements Serializable {
private static final long serialVersionUID = 2023018955366272409L;
private String name;
private long length;
private byte[] content;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
}
package com.wxl.app.file;
import java.io.IOException;
public interface FileService {
public FileBean download() throws IOException;
public void upload(FileBean file);
}
package com.wxl.app.file;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class FileServiceImpl implements FileService {
public FileBean download() throws IOException {
FileBean bean = new FileBean();
File file = new File("c:/看见-柴静(精校版).pdf");
byte[] content = FileUtils.readFileToByteArray(file);
long length = FileUtils.sizeOf(file);
String name = file.getName();
bean.setName(name);
bean.setContent(content);
bean.setLength(length);
return bean;
}
public void upload(FileBean file) {
}
}
package com.wxl.app.file;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.HttpTransport;
public class FileClient {
public static void main(String[] args) throws IOException {
Service serviceModel = new ObjectServiceFactory().create(
FileService.class, null, "http://test/FileService", null);
FileService service = (FileService) new XFireProxyFactory()
.create(serviceModel,
"http://localhost:8080/book/services/FileService");
Client client = Client.getInstance(service);
client.setProperty("mtom-enabled", "true");
client.setProperty(HttpTransport.CHUNKING_ENABLED, "true");
FileBean bean = service.download();
System.out.println(bean.getName());
System.out.println(bean.getLength());
FileUtils.writeByteArrayToFile(new File("d:/" + bean.getName()), bean
.getContent());
}
}
<service>
<name>FileService</name>
<namespace>http://test/FileService</namespace>
<serviceClass>com.wxl.app.file.FileService</serviceClass>
<implementationClass>com.wxl.app.file.FileServiceImpl</implementationClass>
<properties>
<property key="mtom-enabled">true</property>
</properties>
</service>