java cxf文件上传下载,CXF实现WebService进行文件上传

本文档展示了如何使用Java创建一个文件上传服务。包括 Resume 类用于存储候选人姓名、简历类型和数据处理器,ResumeUploadService 接口定义上传方法,ResumeUploadServiceImpl 实现服务端的上传逻辑,将接收到的文件保存到本地。客户端通过 JaxWsProxyFactoryBean 发布并调用服务,完成文件上传。
摘要由CSDN通过智能技术生成

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...........................................发布接口

address="/ResumeUpload">

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值