java常用的工具方法
常用的工具方法
字节数组转16进制字符串
```
1. public static String toHexString(byte[] data) {
if(data == null) {
return null;
}
StringBuilder r = new StringBuilder(data.length*2);
for ( byte b : data) {
r.append(hexCode[(b >> 4) & 0xF]);
r.append(hexCode[(b & 0xF)]);
}
return r.toString();
}
```
加密算法
1.SHA1
private static byte[] getSHA1Digest(String data) throws IOException {
byte[] bytes = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
bytes = md.digest(data.getBytes("UTF-8"));
} catch (GeneralSecurityException gse) {
throw new IOException(gse);
}
return bytes;
}
进制之间的转换
1.二进制转十六进制字符串
/**
* 二进制转十六进制字符串
*
* @param bytes
* @return
*/
private static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
```
2.将十六进制数组转为二进制字节数组
/**
* 将十六进制数组转为二进制字节数组
* @Title hexToBt4
* @param hex
*/
public static String hexToBt4(String hex){
String binary = "";
if (hex.equals("0"))
{
binary = "0000";
}
else if (hex.equals("1"))
{
binary = "0001";
}
if (hex.equals("2"))
{
binary = "0010";
}
if (hex.equals("3"))
{
binary = "0011";
}
if (hex.equals("4"))
{
binary = "0100";
}
if (hex.equals("5"))
{
binary = "0101";
}
if (hex.equals("6"))
{
binary = "0110";
}
if (hex.equals("7"))
{
binary = "0111";
}
if (hex.equals("8"))
{
binary = "1000";
}
if (hex.equals("9"))
{
binary = "1001";
}
if (hex.equals("A"))
{
binary = "1010";
}
if (hex.equals("B"))
{
binary = "1011";
}
if (hex.equals("C"))
{
binary = "1100";
}
if (hex.equals("D"))
{
binary = "1101";
}
if (hex.equals("E"))
{
binary = "1110";
}
if (hex.equals("F"))
{
binary = "1111";
}
return binary;
}
## 数组排序
1数组各个元素的自然排序
```
/**
* 将数组各个元素的自然排序后拼接成一个用逗号分隔的字符串
* @Title naturalOrdering
*/
public static String naturalOrdering(String[] strArray) {
String resultStr = "";
if (strArray != null && strArray.length > 0)
{
Arrays.sort(strArray);
// System.out.println(strArray);
for (int i = 0; i < strArray.length; i++)
{
resultStr += i == 0 ? strArray[i] : "," + strArray[i];
}
}
return resultStr;
}
```
## 请求
1.post请求,参数为json字符串
/**
* post请求,参数为json字符串
* post请求,参数为json字符串
* @param url 请求地址
* @param jsonString json字符串
* @return 响应
*/
public static String postJson(String url,String jsonString) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
try {
post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
post.setHeader("Content-Type", "application/json");
response = httpClient.execute(post);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
2.向指定 URL 发送POST方法的请求
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, JSONObject json) {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/json");
post.addHeader("Authorization", "Basic YWRtaW46");
String result = "";
try {
String tt = json.toString();
StringEntity s = new StringEntity(json.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(s);
// 发送请求
HttpResponse httpResponse = client.execute(post);
// 获取响应输入流
InputStream inStream = httpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
result = strber.toString();
System.out.println(result);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
System.out.println("请求服务器成功,做相应处理");
} else {
System.out.println("请求服务端失败");
}
} catch (Exception e) {
System.out.println("请求异常");
throw new RuntimeException(e);
}
return result;
}
2.向指定 URL 发送GET方法的请求
/**
* GET方法的请求
* 获取请求接口的返回信息
*
* @param url
* 请求接口时需要传入的URL
*/
public static String sendRequest(String url) {
InputStream inputStream = null;
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try {
URL requestURL = new URL(url);
// 获取连接
httpURLConnection = (HttpURLConnection) requestURL.openConnection();
httpURLConnection.setConnectTimeout(10000); //建立连接的超时时间,毫秒
httpURLConnection.setReadTimeout(25000); //获得返回的超时时间,毫秒
httpURLConnection.setRequestMethod("GET");
// 通过输入流获取请求的内容
inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String temp = null;
StringBuffer stringBuffer = new StringBuffer();
// 循环读取返回的结果
while ((temp = bufferedReader.readLine()) != null) {
stringBuffer.append(temp);
}
return stringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
}finally {
//断开连接
if (httpURLConnection!=null) {
httpURLConnection.disconnect();
}
// 关闭流
if(bufferedReader!=null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}