1、webservice 接口调用可参考jdk安装bin 下wsimport.exe。
2、本实例是CXF调用 .net webservice,且.net webservice接口参数是一个对象。
3、本实例调用的是文件上传,具体参数可忽略
package com.jr.entity.web; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigInteger; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.security.MessageDigest; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.apache.cxf.transport.http.HTTPConduit; import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; public class cxfTest { public static void main(String[] args) { routeServer(); } /** * * cxf 动态调用WebService * * @return */ public static void routeServer() { //测试值开始 String id="id"; String USERID="1400000000"; String KEY="VOWPEP7MBP0ZFQF"; String attachmentSetCode="10000"; int attachmentCount=1; String attachmentCode="10001"; String serialNumber="10002"; String uploadWebHost="http://192.168.0.53"; // url为调用webService的wsdl地址 String url = "http://192.168.0.53:5280/Services/UnS/UnStructuredService?wsdl"; String path="E://lc//website-master.rar";//上传的文件路径 //cxf 调用 webservice JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); // 策略 httpClientPolicy.setConnectionTimeout( 360000 ); //连接超时 httpClientPolicy.setAllowChunking( false ); httpClientPolicy.setReceiveTimeout( 100000 ); //接收超时 Client client = dcf.createClient(url); HTTPConduit http = (HTTPConduit) client.getConduit(); http.setClient(httpClientPolicy); try { File file=new File(path); String fileName=file.getName();//文件名 String suffix = "."+fileName.substring(fileName.lastIndexOf(".") + 1);//文件后缀 // 获取文件流。 InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] fileStream=new byte[fis.available()]; fis.read(fileStream); fis.close(); //System.out.println("字节大小:"+fileStream.length); String MD5=getMd5ByFile(file); //文件的md5 //调用的方法 QName name = new QName("http://www.xxx.com", "ReceiveFile_GCJS"); //反射具体的类 Object FileUploadMessage = Thread.currentThread().getContextClassLoader().loadClass("com.tlbxygw.FileUploadMessage").newInstance(); //Method[] methods= FileUploadMessage.getClass().getMethods(); // 反射所有方法 Method m1 = FileUploadMessage.getClass().getMethod("setID", javax.xml.bind.JAXBElement.class); Method m2 = FileUploadMessage.getClass().getMethod("setUSERID", javax.xml.bind.JAXBElement.class); Method m3 = FileUploadMessage.getClass().getMethod("setKEY", javax.xml.bind.JAXBElement.class); Method m4 = FileUploadMessage.getClass().getMethod("setAttachmentSetCode", javax.xml.bind.JAXBElement.class); Method m5 = FileUploadMessage.getClass().getMethod("setAttachmentCount", java.lang.Integer.class); Method m6 = FileUploadMessage.getClass().getMethod("setAttachmentCode", javax.xml.bind.JAXBElement.class); Method m7 = FileUploadMessage.getClass().getMethod("setAttachmentName", javax.xml.bind.JAXBElement.class); Method m8 = FileUploadMessage.getClass().getMethod("setAttachmentType", javax.xml.bind.JAXBElement.class); Method m9 = FileUploadMessage.getClass().getMethod("setMD5", javax.xml.bind.JAXBElement.class); Method m10 = FileUploadMessage.getClass().getMethod("setAttachmentFileName", javax.xml.bind.JAXBElement.class); Method m11 = FileUploadMessage.getClass().getMethod("setSerialNumber", javax.xml.bind.JAXBElement.class); Method m12 = FileUploadMessage.getClass().getMethod("setFileStream", javax.xml.bind.JAXBElement.class); Method m13 = FileUploadMessage.getClass().getMethod("setUploadWebHost", javax.xml.bind.JAXBElement.class); QName _FileUploadMessageAttachmentFileName_QNAME = new QName("http://www.xxx.com", "attachmentFileName");//http://www.xxx.com接口的命名空间 QName _FileUploadMessageSerialNumber_QNAME = new QName("http://www.xxx.com", "serialNumber"); QName _FileUploadMessageAttachmentSetCode_QNAME = new QName("http://www.xxx.com", "attachmentSetCode"); QName _FileUploadMessageAttachmentCode_QNAME = new QName("http://www.xxx.com", "attachmentCode"); QName _FileUploadMessageAttachmentType_QNAME = new QName("http://www.xxx.com", "attachmentType"); QName _FileUploadMessageID_QNAME = new QName("http://www.xxx.com", "ID"); QName _FileUploadMessageUploadWebHost_QNAME = new QName("http://www.xxx.com", "uploadWebHost"); QName _FileUploadMessageAttachmentName_QNAME = new QName("http://www.xxx.com", "attachmentName"); QName _FileUploadMessageUSERID_QNAME = new QName("http://www.xxx.com", "USERID"); QName _FileUploadMessageMD5_QNAME = new QName("http://www.xxx.com", "MD5"); QName _FileUploadMessageFileStream_QNAME = new QName("http://www.xxx.com", "fileStream"); QName _FileUploadMessageKEY_QNAME = new QName("http://www.xxx.com", "KEY"); m1.invoke(FileUploadMessage,new JAXBElement<String>(_FileUploadMessageID_QNAME, String.class, id)); m2.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageUSERID_QNAME, String.class, USERID)); m3.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageKEY_QNAME, String.class, KEY)); m4.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageAttachmentSetCode_QNAME, String.class, attachmentSetCode)); m5.invoke(FileUploadMessage, 1); m6.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageAttachmentCode_QNAME, String.class, attachmentCode)); m7.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageAttachmentName_QNAME, String.class, fileName)); m8.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageAttachmentType_QNAME, String.class, suffix)); m9.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageMD5_QNAME, String.class, MD5)); m10.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageAttachmentFileName_QNAME, String.class, fileName)); m11.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageSerialNumber_QNAME, String.class, serialNumber)); m12.invoke(FileUploadMessage, new JAXBElement<byte[]>(_FileUploadMessageFileStream_QNAME, byte[].class, fileStream)); m13.invoke(FileUploadMessage, new JAXBElement<String>(_FileUploadMessageUploadWebHost_QNAME, String.class, uploadWebHost)); //调用方法 Object[] objects= client.invoke(name, FileUploadMessage); if (objects!=null && objects.length>0) { //获取返回值 Object UpFileResult=objects[0]; //com.tlbxygw.UpFileResult Method[] arrMethod=UpFileResult.getClass().getMethods(); Method getSN=UpFileResult.getClass().getMethod("getSN"); Method getMessage=UpFileResult.getClass().getMethod("getMessage"); Method isIsSuccess=UpFileResult.getClass().getMethod("isIsSuccess"); JAXBElement<String> SN=(JAXBElement<String>) getSN.invoke(UpFileResult, null); JAXBElement<String> Message=(JAXBElement<String>) getMessage.invoke(UpFileResult, null); String snValue=SN.getValue(); String messageValue=Message.getValue(); //返回的具体值 System.out.println(snValue+"***"+messageValue+"****"+isIsSuccess.invoke(UpFileResult, null)); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取文件的md5值 * @param file * @return * @throws FileNotFoundException */ public static String getMd5ByFile(File file) throws FileNotFoundException { String value = null; FileInputStream in = new FileInputStream(file); try { MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(byteBuffer); BigInteger bi = new BigInteger(1, md5.digest()); value = bi.toString(16); } catch (Exception e) { e.printStackTrace(); } finally { if (null != in) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return value; } }
时间: 2024-10-12 12:08:28