根据网络url图片路径获取图片字节数组工具类
来源:互联网 发布:淘宝无线端链接转化 编辑:程序博客网 时间:2024/06/14 16:25
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();
}
}
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();
}
}
阅读全文
0 0
- 根据网络url图片路径获取图片字节数组工具类
- Android中如何根据图片url路径来获取网络图片
- Android中如何根据图片url路径来获取网络图片
- Android中如何根据图片url路径来获取网络图片
- 根据图片的网络URL获取Bitmap
- 根据url路径获取图片并显示到ListView中
- 根据网络图片地址获取资源的字节流
- 工具类--本地路径图片转字节
- 根据url下载网络图片
- 根据图片url获取图片尺寸
- 根据图片url获取图片尺寸
- 根据图片url获取图片尺寸
- 根据URL获取文件,以采集网络图片为例
- 根据路径获取图片,缩放图片
- Android根据图片路径获取图片名字
- 获取网页图片字节数组
- 根据url多线程请求载入网络图片
- ios根据URL获得网络图片
- 线程
- 锐速破解版linux一键自动安装包
- 【判断两棵二叉排序树是否相同】数据结构实验之查找一:二叉排序树
- 关于intellij idea 2017.3版本与输入法冲突的关系
- redis 秒杀多并发处理
- 根据网络url图片路径获取图片字节数组工具类
- jQuery常用方法(持续更新)
- 顺序表
- 逻辑回归
- Linux字符设备驱动v1.0
- fastDFS安装手册
- cas sso 4.0 集成OAuth,用微信登陆示例
- JMockit入门
- boost之Boost.Foreach遍历