
InputStream 转 String
发布日期:2022-02-10 11:36:52
浏览次数:29
分类:技术文章
本文共 1364 字,大约阅读时间需要 4 分钟。
- /** 将String转换成InputStream */
- public static InputStream StringTOInputStream(String in) throws Exception{
- ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
- return is;
- }
- /** 将InputStream转换成byte数组 */
- public static byte[] InputStreamTOByte(InputStream in) throws IOException{
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] data = new byte[BUFFER_SIZE];
- int count = -1;
- while((count = in.read(data,0,BUFFER_SIZE)) != -1)
- outStream.write(data, 0, count);
- data = null;
- return outStream.toByteArray();
- }
- /** 将byte数组转换成InputStream */
- public static InputStream byteTOInputStream(byte[] in) throws Exception{
- ByteArrayInputStream is = new ByteArrayInputStream(in);
- return is;
- }
- /**
- * 将byte数组转换成String
- * @param in
- * @return
- * @throws Exception
- */
- public static String byteTOString(byte[] in) throws Exception{
- InputStream is = byteTOInputStream(in);
- return InputStreamTOString(is);
- }
/*将InputStream转成指定编码过的String/
- public static String InputStreamTOString(InputStream in) throws Exception{
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] data = new byte[BUFFER_SIZE];
- int count = -1;
- while((count = in.read(data,0,BUFFER_SIZE)) != -1)
- outStream.write(data, 0, count);
- data = null;
- return new String(outStream.toByteArray(),"ISO-8859-1");
- }
转载地址:https://blog.csdn.net/courage89/article/details/8959925 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2023年05月31日 13时29分41秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
MVC写在Model文件夹下,登录注册等页面定义的变量规则,不会被更新实体模型删除...
2019-09-06 22:50:54
手动新建MVC控制器和视图,以及数据显示的问题
2019-09-06 22:50:54
C#Stopwatch的使用,性能测试
2019-09-06 22:50:53
sql的ExecuteScalar(),ExecuteNonQuery()
2019-09-06 22:50:53
消息 245,级别 16,状态 1,第 1 行 在将 varchar 值 '2,8' 转换成数据类型 int 时失败。...
2019-09-06 22:50:52
HTML5 中的新特性:
2019-09-06 22:50:52
C#log4net的使用
2019-09-06 22:50:51
C#虚方法和抽象方法的区别
2019-09-06 22:50:51
MVC:页面提交JQ动态生成的输入框的值得解决方案:
2019-09-06 22:50:50
版本号的作用和使用逻辑
2019-09-06 22:50:50
tftp环境搭建
2019-09-06 22:50:49
开发板与PC的SSH环境搭建
2019-09-06 22:50:48
Eclipse颜色设置
2019-09-06 22:50:48
linux下设置coredump文件的开关和路径
2019-09-06 22:50:47
gdb学习笔记
2019-09-06 22:50:47
git命令学习笔记
2019-09-06 22:50:46
VMware共享文件夹编译出现“Value too large for defined data type”错误的解决办法
2019-09-06 22:50:46
「译」通过Fragment处理配置变化
2019-09-06 22:50:45
Android M Dialer完全总结
2019-09-06 22:50:45
「译」Fragment事务与Activity状态丢失
2019-09-06 22:50:44