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”