---
title: "文件上传下载InputStream与string，bytes互转"
author: "fireflylover"
date: "2012-08-15T21:24:18Z"
tags: []
categories: ["Web前端"]
slug: "inputstream-string-bytes"
draft: false
lastmod: "2012-08-15T21:25:05Z"
id: 1633309
url: "https://fireflylover.iteye.com/blog/1633309"
---

1\. 上传文件InputStream转string或bytes

 

 

```
	private byte [] inputStreamToByte(InputStream is) throws IOException { 
        ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream(); 
        int ch; 
        while((ch = is.read() ) != -1){ 
            bAOutputStream.write(ch); 
        } 
        byte data [] =bAOutputStream.toByteArray(); 
        bAOutputStream.close(); 
        return data; 
    } 
	
	private String inputStreamToString(InputStream is) throws IOException { 
        ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream(); 
        int ch; 
        while((ch = is.read() ) != -1){ 
            bAOutputStream.write(ch); 
        } 
        String data = bAOutputStream.toString(); 
        bAOutputStream.close(); 
        return data; 
    }
```

 

2\. 下载 string转 InputStream  

 

 

 

```
public InputStream getInputStream() throws Exception {
          return new ByteArrayInputStream(org.getFileContent().getBytes("UTF-8"));
}
		
注： org.getFileContent() 数据库clob类型，hibernate对应“text” ，pojo对应“String”
```