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异常!");
}
}
}
xuewei97
- 粉丝: 0
- 资源: 66