首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

CXF兑现WebService进行文件上传

2013-07-11 
CXF实现WebService进行文件上传1......................创建上传文件对象类?[java]?view plaincopyprint??

CXF实现WebService进行文件上传

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

?

[java]?view plaincopyprint??
  1. package?fileUpLoad;??
  2. ??
  3. import?javax.activation.DataHandler;??
  4. ??
  5. public?class?Resume?{??
  6. ??
  7. ????private?String?candidateName;???
  8. ????private?String?resumeFileType;???
  9. ????private?DataHandler?resume;??
  10. ????public?String?getCandidateName()?{??
  11. ????????return?candidateName;??
  12. ????}??
  13. ????public?void?setCandidateName(String?candidateName)?{??
  14. ????????this.candidateName?=?candidateName;??
  15. ????}??
  16. ????public?String?getResumeFileType()?{??
  17. ????????return?resumeFileType;??
  18. ????}??
  19. ????public?void?setResumeFileType(String?resumeFileType)?{??
  20. ????????this.resumeFileType?=?resumeFileType;??
  21. ????}??
  22. ????public?DataHandler?getResume()?{??
  23. ????????return?resume;??
  24. ????}??
  25. ????public?void?setResume(DataHandler?resume)?{??
  26. ????????this.resume?=?resume;??
  27. ????}??
  28. ??????
  29. }??


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

?

?

[java]?view plaincopyprint??
  1. package?fileUpLoad;??
  2. ??
  3. import?javax.jws.WebMethod;??
  4. import?javax.jws.WebParam;??
  5. import?javax.jws.WebService;??
  6. ??
  7. @WebService??
  8. @javax.xml.ws.soap.MTOM??
  9. public?interface?ResumeUploadService?{??
  10. ????@WebMethod??
  11. ????public?void?uploadResume(@WebParam(name?=?"resume")?Resume?resume);??
  12. }??


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

?

?

[java]?view plaincopyprint??
  1. package?fileUpLoad;??
  2. ??
  3. import?java.io.File;??
  4. import?java.io.FileOutputStream;??
  5. import?java.io.IOException;??
  6. import?java.io.InputStream;??
  7. import?java.io.OutputStream;??
  8. ??
  9. import?javax.activation.DataHandler;??
  10. ??
  11. public?class?ResumeUploadServiceImpl?implements?ResumeUploadService?{??
  12. ??
  13. ????public?void?uploadResume(Resume?resume)?{??
  14. ??
  15. ????????System.out.println("1");??
  16. ????????DataHandler?handler?=?resume.getResume();???
  17. ????????try?{???
  18. ????????????System.out.println("2");??
  19. ????????InputStream?is?=?handler.getInputStream();???
  20. ????????OutputStream?os?=?new?FileOutputStream(new?File("G:\"???
  21. ????????+?resume.getCandidateName()?+"."+???
  22. ????????resume.getResumeFileType()));???
  23. ????????byte[]?b?=?new?byte[100000];???
  24. ????????int?bytesRead?=?0;???
  25. ????????while?((bytesRead?=?is.read(b))?!=?-1)?{???
  26. ????????os.write(b,?0,?bytesRead);???
  27. ????????}???
  28. ????????System.out.println("3");??
  29. ????????os.flush();???
  30. ????????os.close();???
  31. ????????is.close();???
  32. ????????}?catch?(IOException?e){??
  33. ????????????e.printStackTrace();??????????
  34. ????????}??
  35. ????}??
  36. }??


4...........................................发布接口

[java]?view plaincopyprint??
  1. <!--?实现文件上传接口?-->??
  2. <jaxws:server?id="ResumeUpload"?serviceClass="fileUpLoad.ResumeUploadService"??
  3. ????address="/ResumeUpload">??
  4. ????<!--?添加实现类?-->??
  5. ????<jaxws:serviceBean>??
  6. ????????<ref?bean="ResumeUploadServiceImpl"?/>??
  7. ????</jaxws:serviceBean>??
  8. ????<!--?添加协议?使用MTOM附件-->??
  9. ????<jaxws:properties>??
  10. ????????<entry?key="mtom-enabled"?value="true"?/>??
  11. ????</jaxws:properties>??
  12. </jaxws:server>??



?

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

?

[java]?view plaincopyprint??
  1. package?fileUpLoad;??
  2. ??
  3. import?java.io.File;??
  4. ??
  5. import?javax.activation.DataHandler;??
  6. import?javax.activation.DataSource;??
  7. import?javax.activation.FileDataSource;??
  8. ??
  9. import?org.apache.cxf.jaxws.JaxWsProxyFactoryBean;??
  10. ??
  11. public?class?UpLoad?{??
  12. ????public?static?void?main(String[]?args)?throws?Exception?{??
  13. ??????????
  14. ????????String?url?=?"http://localhost:8080/SpringCXF/services/ResumeUpload?wsdl";??
  15. ?????????Resume?resume?=?new?Resume();??
  16. ?????????resume.setCandidateName("ss");??
  17. ?????????resume.setResumeFileType("jpg");??
  18. ?????????DataSource?source?=?new?FileDataSource(new?File("d:\\中国.jpg"));??
  19. ?????????resume.setResume(new?DataHandler(source));???
  20. ??
  21. ?????????JaxWsProxyFactoryBean?factory?=?new?JaxWsProxyFactoryBean();??
  22. ???????????
  23. ?????????factory.setServiceClass(ResumeUploadService.class);??
  24. ?????????factory.setAddress(url);??
  25. ?????????ResumeUploadService?client?=?(ResumeUploadService)?factory.create();??
  26. ?????????try?{??
  27. ?????????????client.uploadResume(resume);??
  28. ????????}?catch?(Exception?e)?{??
  29. ????????????System.out.println("sa");??
  30. ????????}??????????
  31. ???????????
  32. ?????????System.out.println("success");??
  33. ????}??
  34. ??
  35. }??


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

?

?

热点排行