⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileuploadserviceclientimpl.java

📁 Java使用webservice上传文件的例子
💻 JAVA
字号:
package com.jones.web.service.client.impl;

import java.net.MalformedURLException;

import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.fault.XFireFault;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.HttpTransport;

import com.jones.web.service.FileUpLoadService;
import com.jones.web.service.authentication.impl.ClientAuthHandler;
import com.jones.web.service.client.FileUpLoadServiceClient;
import com.jones.web.service.model.UploadFile;
import com.jones.web.services.exception.WebServiceAccessException;

/**
 * 文件上传web服务客户端调用API默认实现类
 * 
 * @author gubiao
 * 
 */
public class FileUpLoadServiceClientImpl implements FileUpLoadServiceClient {

	private String username;

	private String password;
	
	public FileUpLoadServiceClientImpl() {

	}
	
	public FileUpLoadServiceClientImpl(String username, String password) {

		this.username = username;
		this.password = password;

	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	/**
	 * 调用指定主机的特定端口提供的web服务进行文件上传
	 * 
	 * @author gubiao
	 * 
	 * @param String
	 *            hostAddress 主机名或IP地址
	 * 
	 * @param int
	 *            port 端口
	 * 
	 * @param UploadFile
	 *            file 待上传的文件
	 * 
	 * @return boolean 方法回执
	 * 
	 * @throws WebServiceAccessException
	 *             web服务访问异常
	 */
	public boolean upload(String hostAddress, int port, UploadFile file)
			throws WebServiceAccessException {

		try {

			Service serviceModel = new ObjectServiceFactory().create(
					FileUpLoadService.class, "jones",
					"http://services.web.jones.com", null);

			FileUpLoadService service = (FileUpLoadService) new XFireProxyFactory()
					.create(serviceModel, "http://" + hostAddress + ":" + port
							+ "/FileUpLoadService/services/FileUpLoadService");

			Client client = Client.getInstance(service);
			client.setProperty("mtom-enabled", "true");
			client.setProperty(HttpTransport.CHUNKING_ENABLED, "true");
			client.addOutHandler(new ClientAuthHandler(this.username,this.password));

			boolean result = service.upload(file);

			return result;

		} catch (MalformedURLException e) {

			e.printStackTrace();

			throw new WebServiceAccessException("无法连接到web服务!");

		} catch (XFireFault e) {

			e.printStackTrace();

			throw new WebServiceAccessException("服务器端XFireFault异常!");

		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -