从一个字节输入流获取输入,将字节写入到ByteArrayOutputStream,最后返回所有字节
public static byte[] getFileStringFromOSS(String fileUrl){
OSSClient ossClient =getOSSClient();
OSSObject ossObject =null;
logger.info("从"+fileUrl+"下载文件");
System.out.println("从"+fileUrl+"下载文件");
ossObject =ossClient.getObject(bucket,fileUrl);
byte[]from =new byte[1024*4];
byte[] in2b =null;
InputStream inputStream = ossObject.getObjectContent();
ByteArrayOutputStream byteOut =new ByteArrayOutputStream();
int len=0;
try{
while ((len =inputStream.read(from))!=-1) {
byteOut.write(from,0,len);
in2b =byteOut.toByteArray();
}
System.out.println("文件读取完毕");
}catch (IOException e){
System.out.println("文件读取错误");
e.printStackTrace();
return null;
}finally {
try {
byteOut.close();
inputStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
return in2b;
}