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