加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

CXF实现WebService进行文件上传

发布时间:2020-12-17 00:40:49 所属栏目:安全 来源:网络整理
导读:1......................创建上传文件对象类 package fileUpLoad;import javax.activation.DataHandler;public class Resume {private String candidateName; private String resumeFileType; private DataHandler resume;public String getCandidateName()

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,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://www.voidcn.com/article/p-pbdmvchg-pe.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读