---
title: "xfire webservice实现附件上传"
author: "wentry"
date: "2013-07-11T15:31:24Z"
tags: []
categories: ["编程语言"]
slug: "xfire-webservice"
draft: false
lastmod: "2013-07-05T17:10:21Z"
id: 1900188
url: "https://wentry.iteye.com/blog/1900188"
---

不再集成spring，只需ifire即可。

思路是把上传文件转换成流，然后通过传byte来实现附件的上传.
在附件上传之前，需要鉴权，
soap方式鉴权。

功能看附件代码，可直接运行。


部分代码：
服务端：
	public UserInfo editNews(NewsInfo info, List<UploadFileInfo> fileList) {
		if(fileList==null||fileList.size()<1){
			
		}else{
			for(UploadFileInfo uploadFileInfo:fileList){
				// 这里调用文件处理类来保存文件
				FileUtil.byteToFile(uploadFileInfo.getBytes(), "D:/log", uploadFileInfo.getFileName());
			}
			
		}
		
		System.out.println(""+info.getNewsTitle());
		System.out.println(""+info.getNewsUrl());
		System.out.println(""+info.getNewsText());


		UserInfo u = new UserInfo();
		u.setName(info.getNewsTitle()+","+info.getNewsUrl());
		u.setNum(222);
		return u;

	} 


//
客户端代码：

	public  void uploadFile() throws IOException{
		String url="http://localhost:8080/x1/services/HelloWord";


		Service serviceModel = new ObjectServiceFactory().create(HelloWorldS.class);
		HelloWorldS service = (HelloWorldS) new XFireProxyFactory().create(serviceModel,url);
		XFireProxy proxy = (XFireProxy)Proxy.getInvocationHandler(service);
		Client client = proxy.getClient();
		client.addOutHandler(new ClientAuthenticationHandler("abcd","1234"));//鉴权用户名和密码
		
		 List<UploadFileInfo> fileList = new ArrayList<UploadFileInfo>();
		File file = new File("D:/my");
		File files = file.listFiles();
		for(File f:files){
			if(f.isFile()){
				byte bytes =FileUtil.getBytesFromFile(f);
				 UploadFileInfo uplpadFileInfo = new UploadFileInfo();
				 uplpadFileInfo.setBytes(bytes);
				 uplpadFileInfo.setFileName(f.getName());
				 fileList.add(uplpadFileInfo);
			}
		}

		 NewsInfo newsInfo = new NewsInfo();
		 newsInfo.setNewsTitle("newsTitle");
		 newsInfo.setNewsUrl("newsUrl");
		 newsInfo.setNewsText("newsText");
		 
		 
		 UserInfo userInfo = service.editNews(newsInfo, fileList);   
		 
		 System.out.print(userInfo.getName()+","+userInfo.getNum());
		

	}