一:inputStream转换

1、inputStream转为byte

//方法一  org.apache.commons.io.IOUtils包下的实现(建议)
IOUtils.toByteArray(inputStream);
//方法二 用java代码实现(其实就是对上面方法一的解析)
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}

2、inputStream转为file

    public static void inputstreamtofile(InputStream ins, File file) throws IOException {
OutputStream os = new FileOutputStream(file);
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}

3、inputStream转为String

//方法一  使用org.apache.commons.io.IOUtils包下的方法
IOUtils.toString(inputStream); //方法二
/**
* 将InputStream转换成某种字符编码的String
*
* @param in
* @param encoding
* @return
* @throws Exception
*/
public static String inputStreamToString(InputStream in, String encoding) {
String string = null;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
try {
while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
} catch (IOException e) {
logger.error("io异常", e.fillInStackTrace());
} try {
string = new String(outStream.toByteArray(), encoding);
} catch (UnsupportedEncodingException e) {
logger.error("字符串编码异常", e.fillInStackTrace());
}
return string;
}

二:byte[]转换

1、byte[]转为inputStream

InputStream sbs = new ByteArrayInputStream(byte[] buf); 

2、byte[]转为File

  public static void byte2File(byte[] buf, String filePath, String fileName)
{
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try
{
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory())
{
dir.mkdirs();
}
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (bos != null)
{
try
{
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

3、byte[]转String

//先将byte[]转为inputStream,然后在转为String
InputStream is = new ByteArrayInputStream(byte[] byt);
//然后在根据上文提到的将inputStream转为String的方法去转换

三、File的转换

1、file转inputStream

FileInputStream fileInputStream = new FileInputStream(file);

最新文章

  1. iOS证书问题
  2. MySQL 安装 + 精简 + 配置
  3. hadoop fs -mkdir testdata错误 提示No such file or directory
  4. junit基础篇、中级篇-实例代码
  5. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java
  6. mysql5.6安装
  7. linux常用命令收集(持续中)
  8. Mac 使用phpMyAdmin
  9. 成功解决Tomcat-JDBC-MySQL乱码
  10. jquery实现asp.net 网页鼠标所在位置
  11. mysql主从复制的配置总结
  12. jquery回调函数的一个案例
  13. Centos7安装Tair及配置测试
  14. balanced binary tree(判断是否是平衡二叉树)
  15. Win7下 Python中文正则的奇异表现
  16. Web3与智能合约交互实战
  17. Failed to start /etc/rc.d/rc.local Compatibility
  18. JS应用猜数游戏
  19. NTP时间服务器实战应用详解-技术流ken
  20. Java:内部接口

热门文章

  1. python 之 决策树分类算法
  2. php实现socket
  3. Android RIL Architecture
  4. 关于博主noble_
  5. BI开发(ETL-DW)
  6. Python:23种Pandas核心操作
  7. 经典算法 Manacher算法详解
  8. Python之函数——基础篇
  9. Python Twisted系列教程1:Twisted理论基础
  10. [Flutter] 发布自己的插件 package