ITeye博客

文件上传下载InputStream与string,bytes互转

fireflylover · 2012-08-15 21:24 · 文件操作 · Web前端 · Markdown

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”

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。