1. 程式人生 > >根據網路url圖片路徑獲取圖片位元組陣列工具類

根據網路url圖片路徑獲取圖片位元組陣列工具類

package com.hzy.oss.util;


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;


import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.UriUtils;


/**
 * 簡單的請求工具類
 * 
 * @author liutianlong
 *
 */
public final class SimpleRequestHelper
{
// 預設編碼型別
private final static String DEFAULT_CHARSET = "UTF-8";
// GET請求方式
private final static String METHOD_GET = "GET";
// POST請求方式
private final static String METHOD_POST = "POST";
// 預設超時時間
private final static int DEFAULT_TIME_OUT = 3000;
// URL驗證正則表示式
private final static String URL_VALIDATE_REGEX = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
// 正則表示式驗證器
private final static Pattern PATTERN = Pattern.compile(URL_VALIDATE_REGEX);


/**
* GET請求

* @param url
* @param params
* @return
*/
public final static byte[] doGet(String url, Map<String, String> params)
throws Exception
{
return doRequest(concatGetParams(url, params), params, METHOD_GET);
}


/**
* POST請求

* @param url
* @param params
* @return
*/
public final static byte[] doPost(String url, Map<String, String> params)
throws Exception
{
return doRequest(url, params, METHOD_POST);
}


private final static byte[] doRequest(String urlStr,
Map<String, String> params, String method) throws Exception
{
InputStream is = null;
ByteArrayOutputStream os = null;
byte[] buff = new byte[1024];
int len = 0;
try
{
URL url = new URL(UriUtils.encodePath(urlStr, DEFAULT_CHARSET));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "plain/text;charset="
+ DEFAULT_CHARSET);
conn.setRequestProperty("charset", DEFAULT_CHARSET);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod(method);
conn.setReadTimeout(DEFAULT_TIME_OUT);
conn.connect();
if (METHOD_POST.equalsIgnoreCase(method))
{
setPostParams(conn, params);
}
is = conn.getInputStream();
os = new ByteArrayOutputStream();
while ((len = is.read(buff)) != -1)
{
os.write(buff, 0, len);
}
} catch (IOException e)
{
throw e;
} finally
{
if (is != null)
{
try
{
is.close();
} catch (IOException e)
{
}
}
if (os != null)
{
try
{
os.close();
} catch (IOException e)
{
}
}
}
return os.toByteArray();
}


private final static String concatGetParams(String url,
Map<String, String> params)
{
if (StringUtils.isBlank(url))
{
return null;
}
if (params != null)
{
Set<Map.Entry<String, String>> set = params.entrySet();
Iterator<Map.Entry<String, String>> iterator = set.iterator();
Map.Entry<String, String> entry = null;
String name = null;
while (iterator.hasNext())
{
entry = iterator.next();
name = entry.getKey() + "=" + entry.getValue();
if (!url.contains("?"))
{
url += "?";
}
url += "&" + name;
}
}
return url;
}


private final static void setPostParams(HttpURLConnection conn,
Map<String, String> params)
{
if (params == null)
{
return;
}
// 建立輸入流,向指向的URL傳入引數
DataOutputStream dos = null;
try
{
dos = new DataOutputStream(conn.getOutputStream());
Set<Map.Entry<String, String>> set = params.entrySet();
Iterator<Map.Entry<String, String>> iterator = set.iterator();
Map.Entry<String, String> entry = null;
while (iterator.hasNext())
{
entry = iterator.next();
dos.writeBytes(entry.getKey() + "="
+ URLEncoder.encode(entry.getValue(), "UTF-8"));
}
} catch (IOException e)
{


} finally
{
if (dos != null)
{
try
{
dos.flush();
dos.close();
} catch (IOException e)
{
}
}
}
}


/**
* 驗證是否是URL

* @param url
* @return
*/
public final static boolean isURL(String url)
{
return PATTERN.matcher(url).matches();
}
}