01: /**
02: *
03: */package com.bostechcorp.cbesb.ui.util;
04:
05: import java.io.File;
06: import java.io.FileOutputStream;
07: import java.io.IOException;
08: import java.io.InputStream;
09: import java.io.OutputStream;
10: import java.net.HttpURLConnection;
11: import java.net.MalformedURLException;
12: import java.net.URL;
13:
14: import org.apache.commons.logging.Log;
15: import org.apache.commons.logging.LogFactory;
16:
17: /**
18: * @author LPS
19: *
20: */
21: public class DownloadUtil {
22: Log logger = LogFactory.getLog(DownloadUtil.class);
23:
24: public enum HttpRequestMode {
25: GET, POST
26: };
27:
28: public String httpSimpleFetch(String urlLocation,
29: HttpRequestMode rm, File file)
30: throws MalformedURLException, IOException {
31: URL url = new URL(urlLocation);
32: HttpURLConnection huc = (HttpURLConnection) url
33: .openConnection();
34: huc.setRequestMethod(rm.toString());
35: huc.connect();
36: InputStream is = huc.getInputStream();
37: StringBuffer response = new StringBuffer();
38: int code = huc.getResponseCode();
39: logger.debug(urlLocation + " response code is:" + code);
40: if (code >= 200 && code < 300) {
41: int ch;
42: while ((ch = is.read()) > 0) {
43: response.append((char) ch);
44: }
45: huc.disconnect();
46: //put string into the file
47:
48: OutputStream os = new FileOutputStream(file);
49: os.write(response.toString().getBytes());
50: os.close();
51: return response.toString();
52: }
53: return "";
54: }
55:
56: // public static void main(String[] args)
57: // {
58: // try {
59: // new DownloadUtil()
60: // .httpSimpleFetch(
61: // "http://www.pacificspirit.com/"Authoring/wsdl/YahooV1Search.wsdl",
62: // HttpRequestMode.GET, new File("c:\\asd\\asd.wsdl"));
63: // } catch (MalformedURLException e) {
64: // // TODO Auto-generated catch block
65: // e.printStackTrace();
66: // } catch (IOException e) {
67: // // TODO Auto-generated catch block
68: // e.printStackTrace();
69: // }
70: // }
71: }
|