读取字节流遇到的坑

从一个字节输入流获取输入,将字节写入到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;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容