Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.rt.core.util.IOUtil Maven / Gradle / Ivy
package com.rt.core.util;
import com.json.JSONArray;
import com.rt.core.beans.IOCallback;
import com.rt.core.beans.IOItem;
import java.util.Map;
/**
* IO Net 工具类
*
* @author msc
*/
public class IOUtil extends org.apache.commons.io.IOUtils {
/**
* http get
*
* @param url url
* @param connectionTimeout connectionTimeout
* @param checkSsl checkSsl
* @return String
* @throws Exception exception
*/
public static String doGet(String url, int connectionTimeout, boolean checkSsl)
throws Exception {
return doGet(url, null, connectionTimeout, checkSsl);
}
/**
* http get
*
* @param url url
* @param param param
* @param connectionTimeout connectionTimeout
* @param checkSsl checkSsl
* @return String
* @throws Exception exception
*/
public static String doGet(String url, Map param, int connectionTimeout, boolean checkSsl)
throws Exception {
return doGet(url, null, param, HttpUtil.DEFAULT_CHARSET,
connectionTimeout, checkSsl);
}
/**
* http get
*
* @param url url
* @param headers headers
* @param param param
* @param checkSsl checkSsl
* @param encoding encoding
* @param connectionTimeout connectionTimeout
* @return String
* @throws Exception Exception
*/
public static String doGet(String url, Map headers, Map param,
String encoding, int connectionTimeout, boolean checkSsl) throws Exception {
return HttpUtil.doGet4String(url, headers, param, encoding,
connectionTimeout, checkSsl);
}
/**
* http get
*
* @param url url
* @param headers headers
* @param param param
* @param encoding encoding
* @param connectionTimeout connectionTimeout
* @param checkSsl checkSsl
* @return String
* @throws Exception Exception
*/
public static byte[] doGetToByteArray(String url, Map headers, Map param,
String encoding, int connectionTimeout, boolean checkSsl) throws Exception {
return HttpUtil.doGet4ByteArray(url, headers, param, encoding,
connectionTimeout, checkSsl);
}
/**
* http post提交
*
* @param url url
* @param params params
* @param connectionTimeout connectionTimeout
* @param checkSsl checkSsl
* @return HttpResponse
* @throws Exception Exception
*/
public static String doPost(String url, Map params, int connectionTimeout, boolean checkSsl)
throws Exception {
if (isMultipartParam(params)) {
return HttpUtil.doMultipartPost(url, null, params, null,
connectionTimeout, checkSsl);
} else {
return HttpUtil.doPost4String(url, null, params, null,
connectionTimeout, checkSsl);
}
}
/**
* http post提交
*
* @param url url
* @param headers headers
* @param param param
* @param encoding encoding
* @param connectionTimeout connectionTimeout
* @param checkSsl checkSsl
* @return String
* @throws Exception Exception
*/
public static String doPost(String url, Map headers,
Map param, String encoding, int connectionTimeout, boolean checkSsl) throws Exception {
if (isMultipartParam(param)) {
return HttpUtil.doMultipartPost(url, headers, param, encoding,
connectionTimeout, checkSsl);
} else {
return HttpUtil.doPost4String(url, headers, param, encoding,
connectionTimeout, checkSsl);
}
}
/**
* http post提交
*
* @param url url
* @param headers headers
* @param param param
* @param requestBody requestBody
* @param encoding encoding
* @param connectionTimeout connectionTimeout
* @param checkSsl checkSsl
* @return String
* @throws Exception Exception
*/
public static String doPost(String url, Map headers,
Map param, String requestBody, String encoding,
int connectionTimeout, boolean checkSsl) throws Exception {
url = buildUrlParam(url, param);
return HttpUtil.doPostString4String(url, headers, requestBody,
encoding, connectionTimeout, checkSsl, null);
}
public static String doPost4ByteUpload(String url, Map headers,
Map param, byte[] requestBody, String encoding,
int connectionTimeout, boolean checkSsl) throws Exception {
url = buildUrlParam(url, param);
return HttpUtil.doPost4ByteUpload(url, headers, null, requestBody,
encoding, connectionTimeout, checkSsl);
}
/**
* http post提交
*
* @param url url
* @param headers headers
* @param params params
* @param encoding encoding
* @param connectionTimeout connectionTimeout
* @param checkSsl checkSsl
* @return byte[]
* @throws Exception Exception
*/
public static byte[] doPostToByteArray(String url,
Map headers, Map params, String encoding,
int connectionTimeout, boolean checkSsl) throws Exception {
return HttpUtil.doPost4ByteArray(url, headers, params, encoding,
connectionTimeout, checkSsl);
}
/**
* http post提交
*
* @param url url
* @param headers headers
* @param params params
* @param encoding encoding
* @param connectionTimeout connectionTimeout
* @param outputCallback outputCallback
* @param checkSsl checkSsl
* @throws Exception Exception
*/
public static void doPostProcess(String url, Map headers,
Map params, String encoding,
int connectionTimeout, boolean checkSsl,
IOCallback outputCallback) throws Exception {
HttpUtil.doPostProcess(url, headers, params, encoding,
connectionTimeout, checkSsl, outputCallback);
}
/**
* 构造url参数
*
* @param url url
* @param params params
* @return String
*/
private static String buildUrlParam(String url, Map params) {
if (RTUtil.isEmpty(url) || params == null || params.isEmpty()) {
return url;
}
StringBuilder paramString = new StringBuilder(url);
for (String name : params.keySet()) {
if (paramString.indexOf("?") != -1) {
paramString.append("&");
} else {
paramString.append("?");
}
paramString.append(name);
paramString.append("=");
paramString.append(RTUtil.val(params.get(name)));
}
return paramString.toString();
}
/**
* 判断是否有文件上传需求
*
* @param param 参数map
* @return 参数是否包含文件
*/
private static boolean isMultipartParam(Map param) {
for (Object key : param.keySet()) {
Object value = param.get(key);
if (value instanceof IOItem) {
return true;
}
if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
for (int i = 0; i < array.length(); i++) {
Object innerValue = array.get(i);
if (innerValue instanceof IOItem) {
return true;
}
}
}
}
return false;
}
}