当前位置: 代码迷 >> Web前端 >> xfire-附件
  详细解决方案

xfire-附件

热度:575   发布时间:2012-09-27 11:11:17.0
xfire--附件

?

---------spring+xfire进行附件传输

?

----------------------------------Server--------------------------------------------

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

?

import org.apache.log4j.Logger;

import org.codehaus.xfire.fault.XFireFault;

?

import com.cmhit.service.FileUpLoadService;

import com.cmhit.service.model.UploadFile;

?

public class FileUpLoadServiceImpl implements FileUpLoadService {

private static final Logger logger = Logger.getLogger(FileUpLoadServiceImpl.class);

private String fileSavePath;

public void setFileSavePath(String fileSavePath) {

this.fileSavePath = fileSavePath;

}

public boolean upload(UploadFile file)throws XFireFault{

logger.info("开始接收上传的文件...............");

if(file == null && file.getFile() == null){

throw new XFireFault(new Exception("上传文件不能为NULL"));

}

String title = file.getTitle();

?

try {

InputStream in = file.getFile().getInputStream();

if(this.fileSavePath != null && (!this.fileSavePath.equals(""))){

File fileDirectory = new File(this.fileSavePath);

if(!fileDirectory.exists()){

fileDirectory.mkdir();

}

}

OutputStream out = new FileOutputStream(new File(this.fileSavePath + title));

if(in != null){

int s = -1;

while((s = in.read())!= -1){

out.write(s);

}

in.close();

}

out.close();

logger.info("文件:"+title+"上传成功!");

return true;

} catch (IOException e) {

e.printStackTrace();

throw new XFireFault(new Exception("webservice服务产生异常!"));

}

?

}

}

?

?

---------------------------------CLIENT---------------------------------------------


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.cmhit.service.FileUpLoadService;
import com.cmhit.service.authentication.impl.ClientAuthHandler;
import com.cmhit.service.client.FileUpLoadServiceClient;
import com.cmhit.service.model.UploadFile;
import com.cmhit.services.exception.WebServiceAccessException;


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;
}
public boolean upload(String hostAddress, int port, UploadFile file)
throws WebServiceAccessException {

try {

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

FileUpLoadService service = (FileUpLoadService) new XFireProxyFactory()
.create(serviceModel, "http://" + hostAddress + ":" + port
+ "/spring_webservice/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异常!");

}

}

}

?