InputStream转字符串
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13public static String readIoToStr(InputStream is) { String result = null; try { byte[] data = new byte[is.available()]; is.read(data); BASE64Encoder encoder = new BASE64Encoder(); result = encoder.encode(data); } catch (Exception e) { e.printStackTrace(); } return result; }
字符串转InputStream
复制代码
1
2
3
4
5
6
7
8
9public static InputStream strToIo(String str) { try { return new ByteArrayInputStream(str.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
byte[] 转InputStream
复制代码
1
2
3
4public static InputStream byteToIo(byte[] bytes) { return new ByteArrayInputStream(bytes); }
InputStream 转 byte[]
复制代码
1
2
3
4
5
6
7
8
9
10
11
12@SneakyThrows public static byte[] inputStreamTobyte(InputStream inputStream) { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); int available = inputStream.available(); byte[] buff = new byte[available]; //buff用于存放循环读取的临时数据 int rc = 0; while ((rc = inputStream.read(buff, 0, available)) > 0) { swapStream.write(buff, 0, rc); } return swapStream.toByteArray(); }
最后
以上就是坚定雪糕最近收集整理的关于Java-InputStream常用转换(字符串,字节数组)的全部内容,更多相关Java-InputStream常用转换(字符串内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复