1......................创建上传文件对象类
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.........................创建接口类
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.........................服务端实现类
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. "1");
16. DataHandler handler = resume.getResume();
17. try {
18. "2");
19. InputStream is = handler.getInputStream();
20. new FileOutputStream(new File("G:\\"
21. "."+
22. resume.getResumeFileType()));
23. byte[] b = new byte[100000];
24. int bytesRead = 0;
25. while ((bytesRead = is.read(b)) != -1) {
26. 0, bytesRead);
27. }
28. "3");
29. os.flush();
30. os.close();
31. is.close();
32. catch (IOException e){
33. e.printStackTrace();
34. }
35. }
36. }
4...........................................发布接口
1. <!-- 实现文件上传接口 -->
2. <jaxws:server id="ResumeUpload" serviceClass="fileUpLoad.ResumeUploadService"
3. "/ResumeUpload">
4. <!-- 添加实现类 -->
5. <jaxws:serviceBean>
6. "ResumeUploadServiceImpl" />
7. </jaxws:serviceBean>
8. <!-- 添加协议 使用MTOM附件-->
9. <jaxws:properties>
10. "mtom-enabled" value="true" />
11. </jaxws:properties>
12. </jaxws:server>
5...................................客户端上传类
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. "http://localhost:8080/SpringCXF/services/ResumeUpload?wsdl";
15. new Resume();
16. "ss");
17. "jpg");
18. new FileDataSource(new File("d:\\中国.jpg"));
19. new DataHandler(source));
20.
21. new JaxWsProxyFactoryBean();
22.
23. class);
24. factory.setAddress(url);
25. ResumeUploadService client = (ResumeUploadService) factory.create();
26. try {
27. client.uploadResume(resume);
28. catch (Exception e) {
29. "sa");
30. }
31.
32. "success");
33. }
34.
35. }