WSDL工具类

package com.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.wsdl.xml.WSDLWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UplaodWSDLFileUtil {
	private static final Log log = LogFactory.getLog(UplaodWSDLFileUtil.class);

	
	public static void main(String[] args) throws IOException {
		//9.152.49.10:32543
		String wsdlUrl = "http://";
		InputStream inputStream = getUrlInputStream(wsdlUrl);
		
		String result = convertStreamToString(inputStream);
		log.info(result);
		xieFile(result);	
	}
	/**
	 * 根据wsdlurl,返回网页内容
	 * @param wsdlUrl
	 * @return
	 */
	public static InputStream getUrlInputStream(String wsdlUrl) {
		InputStream inputStream = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();//定义字符数组输出流对象
		Definition definition = null;
		WSDLFactory wsdlFactory = null;
		
		try {
			wsdlFactory = WSDLFactory.newInstance();
			WSDLReader reader = wsdlFactory.newWSDLReader();
			definition = reader.readWSDL(wsdlUrl);
			WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
			
			Writer wr = new OutputStreamWriter(bos, "gbk");
			wsdlWriter.writeWSDL(definition, wr);
			inputStream = new ByteArrayInputStream(bos.toString().getBytes());
		} catch (WSDLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return inputStream;
	}
	/**
	 * 将输入流转换成String类型
	 * @param inputStream
	 * @return
	 */
	 public static String convertStreamToString(InputStream inputStream) {    
	       
         BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));    
         StringBuilder sb = new StringBuilder();    
   
         String line = null;    
        try {    
            while ((line = reader.readLine()) != null) {    
                 sb.append(line + "\n");    
             }    
         } catch (IOException e) {    
             e.printStackTrace();    
         } finally {    
            try {    
            	inputStream.close();    
             } catch (IOException e) {    
                 e.printStackTrace();    
             }    
         }    
   
        return sb.toString();    
	  }    
	
	
   /**
    * 将inputStream 转换成byte数组
    * @param input
    * @return
    * @throws IOException
    */
   public static byte[] toByteArray(InputStream input) throws IOException {
	    ByteArrayOutputStream output = new ByteArrayOutputStream();
	    byte[] buffer = new byte[4096];
	    int n = 0;
	    while (-1 != (n = input.read(buffer))) {
	        output.write(buffer, 0, n);
	    }
	    return output.toByteArray();
   } 
   
   
   /**
    * 写文件操作
    */
   public static void xieFile(String str){
	   File file = new File("d:/wsdl", "webservice.xml");  
       try {  
           file.createNewFile(); // 创建文件  
       } catch (IOException e) {  
           e.printStackTrace();  
       }  
 
       // 向文件写入内容(输出流)   
       byte bt[] = new byte[1024];  
       bt = str.getBytes();  
       try {  
           FileOutputStream in = new FileOutputStream(file);  
           try {  
               in.write(bt, 0, bt.length);  
               in.close();  
           } catch (IOException e) {  
               e.printStackTrace();  
           }  
       } catch (FileNotFoundException e) {  
           e.printStackTrace();  
       }  
       try {  
           // 读取文件内容 (输入流)  
           FileInputStream out = new FileInputStream(file);  
           InputStreamReader isr = new InputStreamReader(out);  
           int ch = 0;  
           while ((ch = isr.read()) != -1) {  
               System.out.print((char) ch);  
           }  
       } catch (Exception e) {  
       } 
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值