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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。