字节是二进制数据的单位,1字节(Byte)=8位(bit),byte数组一般用来处理文件流的操作
InputStream is = new FileInputStream(new File("D://a.txt")); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int temp; while ((temp = is.read(bytes)) != -1) { outputStream.write(bytes, 0, temp); //转换后的byte[] byte[] finalBytes = outputStream.toByteArray();
二、通过IOUtils
ObsObject obsObject = obsClient.getObject(bucketName, objectName); InputStream inputStream = obsObject.getObjectContent(); byte[] bytes = null; try { bytes = IOUtils.toByteArray(inputStream); } catch (Exception e) { e.printStackTrace(); } finally { inputStream.close(); return bytes;
这种方式更加快速 位于的包:package org.apache.commons.compress.utils;
文件下载
InputStream input = obsObject.getObjectContent(); // 读取对象内容 byte[] b = new byte[1024]; OutputStream bos = response.getOutputStream(); int len;