cn.net.wanmo.common.http.util.ResUtil Maven / Gradle / Ivy
package cn.net.wanmo.common.http.util;
import cn.net.wanmo.common.charset.CharsetUtil;
import cn.net.wanmo.common.util.IdGen;
import cn.net.wanmo.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Scanner;
public class ResUtil {
private static Logger logger = LoggerFactory.getLogger(ResUtil.class);
/**
* 获取响应的编码格式
* 从请求头 header.put("Content-Type", "application/json; charset=UTF-8"); 获取 charset
*
* @param contentType 响应内容编码
* @return 编码格式, 默认 UTF-8
*/
public static String getResponseCharset(String contentType) {
logger.debug("从请求头中解析请求数据使用的编码: {}", contentType);
String charsetName = null;
if (StringUtil.isBlank(contentType)) {
charsetName = CharsetUtil.UTF8_NAME;
return charsetName;
}
String[] params = contentType.split(";");
for (String param : params) {
param = param.trim();
if (param.startsWith("charset")) {
String[] pair = param.split("=", 2);
if (pair.length == 2) {
if (StringUtil.isNotEmpty(pair[1])) {
charsetName = pair[1].trim();
}
}
break;
}
}
return StringUtil.isBlank(charsetName) ? CharsetUtil.UTF8_NAME : charsetName;
}
/**
* 从流中获取响应字符串
*
* @param stream 流数据
* @param charsetName 字符集
* @return 响应
* @throws IOException
*/
public static String getResAsString(InputStream stream, String charsetName) throws IOException {
return getResAsString_1(stream, charsetName);
}
/**
* 从流中获取响应字符串
*
* @param stream 流数据
* @param charsetName 字符集
* @return 响应
* @throws IOException
*/
private static String getResAsString_1(InputStream stream, String charsetName) throws IOException {
Scanner scanner = new Scanner(stream, charsetName);
StringBuffer buffer = new StringBuffer();
while (scanner.hasNextLine()) {
buffer.append(scanner.nextLine());
}
return buffer.toString();
}
/**
* 从流中获取响应字符串
*
* @param stream 流数据
* @param charsetName 字符集
* @return 响应
* @throws IOException
*/
private static String getResAsString_2(InputStream stream, String charsetName) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream, charsetName));
StringBuffer buffer = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
return buffer.toString();
}
/**
* 从流中获取响应字符串
*
* @param stream 流数据
* @param charset 字符集
* @return 响应
* @throws IOException
*/
private static String getResAsString_3(InputStream stream, String charset) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
StringWriter writer = new StringWriter();
char[] chars = new char[1024];
int count = 0;
while ((count = reader.read(chars)) > 0) {
writer.write(chars, 0, count);
}
return writer.toString();
}
/**
* 从流中获取响应文件
*
* @param stream 流数据
* @return 响应
* @throws IOException
*/
public static File getResAsFile(InputStream stream) throws IOException {
return getResAsFile_1(stream);
}
/**
* 从流中获取响应文件
*
* @param stream 流数据
* @return 响应
* @throws IOException
*/
private static File getResAsFile_1(InputStream stream) throws IOException {
File file = File.createTempFile(IdGen.uuid(), "tmp");
BufferedInputStream bis = new BufferedInputStream(stream);
OutputStream os = new FileOutputStream(file);
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1) {
os.write(arr, 0, len);
os.flush();
}
os.close();
bis.close();
return file;
}
}