您的位置:首页 > 编程语言 > Java开发

常用工具类--仅针对于JAVA

2017-03-11 22:25 183 查看
1.HttpUtils

public class HttpUtils {

private static final Log log = LogFactory.getLog(HttpUtils.class);

public static byte[] get(String url) {
return get(url, 0);
}

public static byte[] get(String url, int timeout) {
HttpClient hc = new HttpClient();
hc.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
hc.getHttpConnectionManager().getParams().setSoTimeout(timeout);
GetMethod method = null;
try {
method = new GetMethod(url);
method.setFollowRedirects(false);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
method.getParams().setParameter(HttpMethodParams.HTTP_URI_CHARSET, "utf-8");
int code = hc.executeMethod(method);
if (code == HttpStatus.SC_OK) {
return method.getResponseBody();
}
} catch (Exception e) {
log.error(e, e);
} finally {
if (method != null) {
method.releaseConnection();
method = null;
}
hc.getHttpConnectionManager().closeIdleConnections(0);
}
return new byte[]{};
}

public static byte[] post(String url, Map<String, String> params) {
return post(url, params, 0);
}

public static byte[] post(String url, Map<String, String> params, int timeout) {
HttpClient hc = new HttpClient();
hc.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
hc.getHttpConnectionManager().getParams().setSoTimeout(timeout);
PostMethod method = null;
try {
method = new PostMethod(url);
method.setFollowRedirects(false);
List<NameValuePair> _params = new ArrayList<NameValuePair>();
if (params != null) {
for (String key : params.keySet()) {
_params.add(new NameValuePair(key, params.get(key)));
}
}
method.setRequestBody(_params.toArray(new NameValuePair[]{}));
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
method.getParams().setParameter(HttpMethodParams.HTTP_URI_CHARSET, "utf-8");
int code = hc.executeMethod(method);
if (code == HttpStatus.SC_OK) {
return method.getResponseBody();
}
} catch (Exception e) {
log.error(e, e);
} finally {
if (method != null) {
method.releaseConnection();
method = null;
}
hc.getHttpConnectionManager().closeIdleConnections(0);
}
return new byte[]{};
}

/**
* post请求参数通过body传入
*
* @param url
* @param content
* @return
*/
public static String postContent(String url, String content, int timeout) {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
RequestEntity requestEntity = null;
try {
requestEntity = new StringRequestEntity(content, "application/json", "utf-8");
} catch (UnsupportedEncodingException e1) {
log.error(e1, e1);
}
method.setRequestEntity(requestEntity);
HttpMethodParams param = method.getParams();
param.setContentCharset("utf-8");
try {
client.executeMethod(method);
return method.getResponseBodyAsString();
} catch (HttpException e) {
log.error(e, e);
} catch (IOException e) {
log.error(e, e);
}
return "";
}

/**
* post请求参数通过body传入
* <p>
* 异常抛出版本
*
* @param url
* @param content
* @return
* @throws IOException
*/
public static String postContent2(String url, String content, int timeout) throws IOException {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
RequestEntity requestEntity = null;
try {
requestEntity = new StringRequestEntity(content, "application/json", "utf-8");
} catch (UnsupportedEncodingException e1) {
log.error(e1, e1);
}
method.setRequestEntity(requestEntity);
HttpMethodParams param = method.getParams();
param.setContentCharset("utf-8");
try {
client.executeMethod(method);
return method.getResponseBodyAsString();
} catch (HttpException e) {
log.error(e, e);
throw e;
} catch (IOException e) {
log.error(e, e);
throw e;
}
}

public static Map<String, String> getParams(ServletRequest request) {
Map<String, String> params = new HashMap<String, String>();
if (request == null) {
return params;
}
Enumeration<String> paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = paramNames.nextElement();
String paramValue = request.getParameter(paramName);
params.put(paramName, paramValue);
}
return params;
}

public static String getParam(ServletRequest request, String paramName, String defaultVal) {
String val = request.getParameter(paramName);
if (StringUtils.isEmpty(val)) {
val = defaultVal;
}
return val;
}

/**
* 拼接url参数
*
* @param url
* @param list
* @return
*/
public static String paramUrl(String url, List<NameValuePair> list) {
try {
GetMethod method = new GetMethod(url);
method.setQueryString(list.toArray(new NameValuePair[]{}));
return method.getURI().toString();
} catch (URIException e) {
log.error(e, e);
throw new RuntimeException("拼接url参数失败");
}
}

/**
* 根据文件url获取文件的流
*/
public static InputStream getInputStreamByUrl(String url) {
try {
URL _url = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) _url.openConnection();
httpURLConnection.setDoInput(true);// 打开读取属性
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(50000);
httpURLConnection.setReadTimeout(50000);
httpURLConnection.connect();
return httpURLConnection.getInputStream();
} catch (Exception e) {
return null;
}
}

/**
* 获得文件的MD516进制字符串
*/
public static String getFileMD5(String url) {
InputStream in = null;
try {
in = HttpUtils.getInputStreamByUrl(url);
MessageDigest digest = MessageDigest.getInstance("MD5");
byte buffer[] = new byte[1024];
int len;
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
return new String(Hex.encodeHex(digest.digest()));
} catch (Exception e) {
} finally {
IOUtils.closeQuietly(in);
}
return "";
}

/**
* @param url
* @return size of the resource or -1 if unknown
*/
public static int getFileSize(String url) {
try {
URL _url = new URL(url);
HttpURLConnection httpconn = (HttpURLConnection) _url.openConnection();
return httpconn.getContentLength();
} catch (Exception e) {
}
return -1;
}

public static String getRemortIP(HttpServletRequest request) {
String realIp = request.getHeader("X-Real-IP");
if (StringUtils.isEmpty(realIp)) {
return request.getRemoteAddr();
}
return realIp;
}

public static String getUserAgent(HttpServletRequest request) {
if (request == null)
return "";
return request.getHeader("User-Agent");
}

public static boolean isIE6(HttpServletRequest request) {
return getUserAgent(request).contains("MSIE 6.0");
}

public static boolean isIE7(HttpServletRequest request) {
return getUserAgent(request).contains("MSIE 7.0");
}

public static boolean isIE8(HttpServletRequest request) {
return getUserAgent(request).contains("MSIE 8.0");
}

public static boolean isLowIE(HttpServletRequest request) {
return isIE6(request) || isIE7(request) || isIE8(request);
}

/**
* 读取http请求内容
*
* @param request
* @return
*/
public static String readContent(HttpServletRequest request) {
BufferedReader br = null;
try {
br = request.getReader();
char[] buf = new char[1024];
int len = 0;
StringWriter sw = new StringWriter();
while ((len = br.read(buf)) != -1) {
sw.write(buf, 0, len);
}
return sw.toString();
} catch (Exception e) {
log.info(e, e);
} finally {
IOUtils.closeQuietly(br);
}
return "";
}

/**
* 对下载文件名进行编码,防止中文乱码
*
* @param filename
* @param request
* @return
*/
public static String encodeFilename(String filename, HttpServletRequest request) {
String agent = request.getHeader("USER-AGENT");
try {
if ((agent != null) && (-1 != agent.indexOf("MSIE"))) {
String newFileName = URLEncoder.encode(filename, "UTF-8");
newFileName = StringUtils.replace(newFileName, "+", "%20");
if (newFileName.length() > 150) {
newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");
newFileName = StringUtils.replace(newFileName, " ", "%20");
}
return newFileName;
}
if ((agent != null) && (-1 != agent.indexOf("Mozilla")))
return MimeUtility.encodeText(filename, "UTF-8", "B");

return filename;
} catch (Exception ex) {
return filename;
}
}

/**
* 判断一个请求是否是ajax请求
*
* @param request
* @return
*/
public static boolean isAjax(HttpServletRequest request) {
String xmlHttpRequest = request.getHeader("X-Requested-With");
return StringUtils.equalsIgnoreCase(xmlHttpRequest, "XMLHttpRequest");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: