cn.majingjing.http.client.util.HttpUtils Maven / Gradle / Ivy
package cn.majingjing.http.client.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
/**
* Http工具包
*
* @author MaMarion
* @date 2020/4/24
*/
public class HttpUtils {
/**
* 将输入流转换成字节数组
*
* @param input 输入流
* @return byte[]
* @throws IOException 转换出错将抛出此异常
*/
public static byte[] toByteArray(InputStream input) throws IOException {
if (null == input) {
return new byte[0];
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
/**
* 关闭输入流
*
* @param inputStream 输入流
*/
public static void close(InputStream inputStream) {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException ignored) {
}
}
}
/**
* 关闭输出流
*
* @param outputStream 输出流
*/
public static void close(OutputStream outputStream) {
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException ignored) {
}
}
}
/**
* http状态码是否为成功码(小于400)
*
* @param httpStatus http状态码
* @return boolean <400则返回true,否则返回false
*/
public static boolean isSuccess(int httpStatus) {
return httpStatus == HttpURLConnection.HTTP_OK;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy