采用web service传输超大数据(2)

本文探讨了使用Web Service在xfire中传输超大数据的两种方法:DataHandler接口和byte数组。第一种方法涉及利用DataHandler接口实现高效的数据传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值