我是靠谱客的博主 坚定雪糕,这篇文章主要介绍Java-InputStream常用转换(字符串,字节数组),现在分享给大家,希望可以做个参考。

InputStream转字符串

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public 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
9
public 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
4
public 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常用转换(字符串内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部