CXF实现WebService进行文件上传

1......................创建上传文件对象类

package fileUpLoad;

import javax.activation.DataHandler;

public class Resume {

	private String candidateName; 
	private String resumeFileType; 
	private DataHandler resume;
	public String getCandidateName() {
		return candidateName;
	}
	public void setCandidateName(String candidateName) {
		this.candidateName = candidateName;
	}
	public String getResumeFileType() {
		return resumeFileType;
	}
	public void setResumeFileType(String resumeFileType) {
		this.resumeFileType = resumeFileType;
	}
	public DataHandler getResume() {
		return resume;
	}
	public void setResume(DataHandler resume) {
		this.resume = resume;
	}
	
}

2.........................创建接口类

package fileUpLoad;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
@javax.xml.ws.soap.MTOM
public interface ResumeUploadService {
	@WebMethod
	public void uploadResume(@WebParam(name = "resume") Resume resume);
}

3.........................服务端实现类

package fileUpLoad;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;

public class ResumeUploadServiceImpl implements ResumeUploadService {

	public void uploadResume(Resume resume) {

		System.out.println("1");
		DataHandler handler = resume.getResume(); 
		try { 
			System.out.println("2");
		InputStream is = handler.getInputStream(); 
		OutputStream os = new FileOutputStream(new File("G:\\" 
		+ resume.getCandidateName() +"."+ 
		resume.getResumeFileType())); 
		byte[] b = new byte[100000]; 
		int bytesRead = 0; 
		while ((bytesRead = is.read(b)) != -1) { 
		os.write(b, 0, bytesRead); 
		} 
		System.out.println("3");
		os.flush(); 
		os.close(); 
		is.close(); 
		} catch (IOException e){
			e.printStackTrace();		
		}
	}
}

4...........................................发布接口
	<!-- 实现文件上传接口 -->
	<jaxws:server id="ResumeUpload" serviceClass="fileUpLoad.ResumeUploadService"
		address="/ResumeUpload">
		<!-- 添加实现类 -->
		<jaxws:serviceBean>
			<ref bean="ResumeUploadServiceImpl" />
		</jaxws:serviceBean>
		<!-- 添加协议 使用MTOM附件-->
		<jaxws:properties>
			<entry key="mtom-enabled" value="true" />
		</jaxws:properties>
	</jaxws:server>


5...................................客户端上传类

package fileUpLoad;

import java.io.File;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class UpLoad {
	public static void main(String[] args) throws Exception {
		
		String url = "http://localhost:8080/SpringCXF/services/ResumeUpload?wsdl";
		 Resume resume = new Resume();
		 resume.setCandidateName("ss");
		 resume.setResumeFileType("jpg");
		 DataSource source = new FileDataSource(new File("d:\\中国.jpg"));
		 resume.setResume(new DataHandler(source));	

		 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		 
		 factory.setServiceClass(ResumeUploadService.class);
		 factory.setAddress(url);
		 ResumeUploadService client = (ResumeUploadService) factory.create();
		 try {
			 client.uploadResume(resume);
		} catch (Exception e) {
			System.out.println("sa");
		}		 
		 
		 System.out.println("success");
	}

}

如果出现缺少架包的问题,请参考 http://blog.csdn.net/wangnetkang/article/details/7818399


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值