首页

关于oschrenk-util源码包HttpUtils工具类用于获取网路url资源文件转为字符串及二进制数组数据操作

标签:oschrenk-util,HttpUtils工具类,httputil,url转字符串,url转二进制,URLBuilder,encodeURL,url加密转伪码     发布时间:2018-07-20   

一、前言

关于oschrenk-util源码包中com.oschrenk.net.HttpUtils定义http操作工具类,实现通过网络URL资源地址获取数据流转为字符串文本getTextFile、二进制数组数据getBinaryFile等操作。另外定义com.oschrenk.net.HttpUtils.URLBuilder类,用于对URL地址进行不同参数的build组件、对url的转伪码加密处理encodeURL。

二、源码说明

1.HttpUtils类

package com.oschrenk.net;@b@@b@import java.io.BufferedInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.Reader;@b@import java.net.HttpURLConnection;@b@import java.net.URL;@b@import java.net.URLConnection;@b@@b@public class HttpUtils@b@{@b@  private static final String DEFAULT_ENCODING = "ISO-8859-1";@b@@b@  public static String getTextFile(URL url)@b@    throws IOException@b@  {@b@    HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();@b@@b@    if (urlConnection.getResponseCode() != 200) {@b@      throw new IOException("Wrong response code. Got " + urlConnection.getResponseCode() + ", expected " + 200);@b@    }@b@@b@    String contentType = urlConnection.getContentType();@b@@b@    String encoding = "ISO-8859-1";@b@    int encodingStart = contentType.indexOf("charset=");@b@    if (encodingStart != -1) {@b@      encoding = contentType.substring(encodingStart + 8);@b@    }@b@@b@    InputStream in = new BufferedInputStream(urlConnection.getInputStream());@b@    Reader r = new InputStreamReader(in, encoding);@b@@b@    StringBuilder sb = new StringBuilder();@b@@b@    while ((c = r.read()) != -1) {@b@      int c;@b@      sb.append((char)c);@b@    }@b@@b@    if (r != null)@b@      r.close();@b@    if (in != null)@b@      in.close();@b@@b@    return sb.toString();@b@  }@b@@b@  public static byte[] getBinaryFile(URL url)@b@    throws IOException@b@  {@b@    byte[] data;@b@    URLConnection urlConnection = url.openConnection();@b@    int contentLength = urlConnection.getContentLength();@b@@b@    InputStream bufferedInputStream = null;@b@    try@b@    {@b@      bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());@b@      data = new byte[contentLength];@b@      int bytesRead = 0;@b@      int offset = 0;@b@      while (offset < contentLength) {@b@        bytesRead = bufferedInputStream.read(data, offset, data.length - offset);@b@        if (bytesRead == -1)@b@          break;@b@        offset += bytesRead;@b@      }@b@@b@      if (offset != contentLength)@b@        throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");@b@    }@b@    finally@b@    {@b@      if (bufferedInputStream != null)@b@        bufferedInputStream.close();@b@    }@b@    return data;@b@  }@b@}

2.URLBuilder类

package com.oschrenk.net;@b@@b@import java.io.UnsupportedEncodingException;@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@import java.net.URLEncoder;@b@import java.util.HashSet;@b@import java.util.Set;@b@import org.apache.commons.lang.StringUtils;@b@@b@public class URLBuilder@b@{@b@  public static URL build(URL base, String path)@b@    throws MalformedURLException@b@  {@b@    try@b@    {@b@      return new URL(buildValidUrl(base, path));@b@    } catch (IllegalArgumentException e) {@b@      throw new MalformedURLException(e.getMessage());@b@    }@b@  }@b@@b@  public static Set<URL> build(URL base, Set<String> paths)@b@    throws MalformedURLException@b@  {@b@    Set urls = new HashSet();@b@@b@    for (String s : paths) {@b@      URL url = build(base, s);@b@      if (url != null)@b@        urls.add(url);@b@    }@b@@b@    return urls;@b@  }@b@@b@  private static String buildValidUrl(URL base, String match)@b@  {@b@    if (match.indexOf("javascript:") >= 0) {@b@      throw new IllegalArgumentException("Javascript links don't belong in a href");@b@    }@b@@b@    if (match.indexOf(47) == 0) {@b@      match = base.getAuthority() + match;@b@    }@b@@b@    if (match.indexOf(35) == 0) {@b@      match = base.getAuthority() + base.getPath() + "/" + match;@b@    }@b@@b@    if (match.indexOf("://") == -1) {@b@      match = base.getProtocol() + "://" + match;@b@    }@b@@b@    return match;@b@  }@b@@b@  public static URL encode(URL url)@b@  {@b@    try@b@    {@b@      return new URL(encodeURL(url));@b@    }@b@    catch (MalformedURLException e)@b@    {@b@    }@b@@b@    return url;@b@  }@b@@b@  private static String encodeURL(URL url)@b@  {@b@    StringBuilder sb = new StringBuilder();@b@    try {@b@      sb.append(url.getProtocol());@b@      sb.append("://");@b@      sb.append(url.getAuthority());@b@@b@      String[] crumbs = url.getPath().split("/");@b@      for (int i = 0; i < crumbs.length; ++i)@b@        crumbs[i] = URLEncoder.encode(crumbs[i], "UTF-8");@b@@b@      sb.append(StringUtils.join(crumbs, '/'));@b@    }@b@    catch (UnsupportedEncodingException e) {@b@    }@b@    return sb.toString();@b@  }@b@}