微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关于CXF大文件的传输问题

1.客户端调用webservice工具类


package cn.net.sunge.oas_dep.utils;

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Map;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyfactorybean;
import org.apache.cxf.transport.http.HTTPConduit;

import cn.net.sunge.oas_dep.modules.webservice.model.FileEntity;

public class WsClientUtil {

	public static <T> T getInterface(Class<T> clazz,String address) {
		return getInterface(clazz,address,null);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getInterface(Class<T> clazz,String address,Map<String,Object> properties) {
		JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();
		factory.setAddress(address);
		factory.setServiceClass(clazz);
		if (null != properties) {
			factory.setProperties(properties);
		}
		return (T) factory.create();
	}

	public static <T> T getHttpsInterface(Class<T> clazz,String jksPath,String jksPwd) {
		return getHttpsInterface(clazz,jksPath,jksPwd,null);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getHttpsInterface(Class<T> clazz,String jksPwd,Object> properties) {
		JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();
		factory.setAddress(address);
		factory.setServiceClass(clazz);
		if (null != properties) {
			factory.setProperties(properties);
		}
		T t = (T) factory.create();
		configureSSLOnTheClient(t,jksPwd);
		return t;
	}

	private static void configureSSLOnTheClient(Object obj,String jksPwd) {
		File file = new File(jksPath);

		Client client = ClientProxy.getClient(obj);
		HTTPConduit httpConduit = (HTTPConduit) client.getConduit();

		try {
			TLSClientParameters tlsParams = new TLSClientParameters();
			tlsParams.setdisableCNCheck(true);

			KeyStore keyStore = KeyStore.getInstance("JKS");
			String password = jksPwd;
			String storePassword = jksPwd;

			keyStore.load(new FileInputStream(file),storePassword.tochararray());
			TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
			trustFactory.init(keyStore);
			TrustManager[] trustManagers = trustFactory.getTrustManagers();
			tlsParams.setTrustManagers(trustManagers);

			keyStore.load(new FileInputStream(file),storePassword.tochararray());
			KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
			keyFactory.init(keyStore,password.tochararray());
			KeyManager[] keyManagers = keyFactory.getKeyManagers();
			tlsParams.setKeyManagers(keyManagers);

			// FiltersType filtersTypes = new FiltersType();
			// filtersTypes.getInclude().add(".*_EXPORT_.*");
			// filtersTypes.getInclude().add(".*_EXPORT1024_.*");
			// filtersTypes.getInclude().add(".*_WITH_DES_.*");
			// filtersTypes.getInclude().add(".*_WITH_NULL_.*");
			// filtersTypes.getExclude().add(".*_DH_anon_.*");
			// tlsParams.setCipherSuitesFilter(filtersTypes);

			tlsParams.setdisableCNCheck(true);

			httpConduit.setTlsClientParameters(tlsParams);
		} catch (Exception e) {
			e.printstacktrace();
		}
	}

	/**
	 * Construct FileEntity.
	 * 
	 * @param the
	 *            real file path.
	 * @return FileEntity.
	 */
	public static FileEntity constructFileEntity(String filePath) {
		// construct FileEntity
		FileEntity fileEntity = new FileEntity();
		File file = new File(filePath);
		fileEntity.setFileName(file.getName().substring(0,(file.getName().lastIndexOf("."))));
		fileEntity.setFileType(filePath.substring(filePath.lastIndexOf(".") + 1));
		DataSource source = new FileDataSource(file);
		DataHandler handler = new DataHandler(source);
		fileEntity.setFile(handler);
		return fileEntity;
	}
}

2.调用Util

Map<String,Object> properties = Maps.newHashMap();
properties.put("mtom-enabled",true);
IntranetFileWebserviceI intranetFileWebservice = WsClientUtil.getIntranetInterface(IntranetFileWebserviceI.class,IntranetFileWebserviceI.address,properties);



 参考资料:

http://yanghui-java.iteye.com/blog/1747643

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐