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 {
String wsdlUrl = "http://";
InputStream inputStream = getUrlInputStream(wsdlUrl);
String result = convertStreamToString(inputStream);
log.info(result);
xieFile(result);
}
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) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return inputStream;
}
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();
}
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) {
}
}
}