将InputStream转换成某种字符编码的String

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class StreamUtils {
public static final int BUFFER_SIZE = 4096;


/**
* 将InputStream装换成某种字符编码的String
*
* @param in
* @param encoding
* @return
*/
public static String InputStreamTOString(InputStream in, String encoding) {
String res = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
try {
while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
outputStream.write(data, 0, count);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
res = new String(outputStream.toByteArray(), encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return res;
}
}
上一篇:torch实现clip_by_tensor操作


下一篇:JAVA——InputStream读取String