首页 > 其他分享 >远程的文件转换成byte数组

远程的文件转换成byte数组

时间:2023-04-17 15:11:11浏览次数:30  
标签:转换成 outputStream buffer inputStream 数组 new byte InputStream

1、使用 OkHttp3 库来将远程的 GIF 文件转换成 InputStream

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
            .url("http://xxxxx/resources/upload/20230414/3_yk_anim_cn_64_1.gif")
            .build();
        Response response = client.newCall(request).execute();
        InputStream inputStream = response.body().byteStream();
        // 这里可以使用 inputStream 进行后续操作
        // 例如将其写入文件或将其作为输入流传递给其他方法

2、使用 ByteArrayOutputStream 类和 InputStream 的 read() 方法来将 InputStream 对象转换为 byte[] 数组。

  InputStream inputStream = // 这里是获取到的 InputStream 对象
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        byte[] byteArray = outputStream.toByteArray();
        // 这里可以使用 byteArray 进行后续操作
        // 例如将其写入文件或将其传递给其他方法

 

标签:转换成,outputStream,buffer,inputStream,数组,new,byte,InputStream
From: https://www.cnblogs.com/javaktolin/p/17325919.html

相关文章

  • es6 数组对象求和
    letlist=[{id:1,price:2},{id:2,price:4},{id:3,price:6},{id:4,price:8},];letres=list.reduce((sumData,key,index,arrData)=>{console.log('a',sumData);//上⼀次调⽤回调时返回的累积值c......
  • 数组4
    #include<string>#include<iostream>usingnamespacestd;inlinevoidtest(constchar*title,boolvalue){ cout<<title<<"returns"<<(value?"true":"false")<<endl;}intmain(){ strings1="DEF&q......
  • python3-bytes
    1、介绍python3中,使用bytes类处理字节数据。2、类classbytes(object):"""bytes(iterable_of_ints)->bytesbytes(string,encoding[,errors])->bytesbytes(bytes_or_buffer)->immutablecopyofbytes_or_bufferbytes(int)->byt......
  • 13 数组基本概述
    1.数组基本概述 2.数组的基本使用 ......
  • NumPy 秘籍中文第二版:二、高级索引和数组概念
    在本章中,我们将介绍以下秘籍:安装SciPy安装PIL调整图像大小比较视图和副本翻转Lena花式索引位置列表索引布尔值索引数独的步幅技巧广播数组简介NumPy以其高效的数组而闻名。之所以成名,部分原因是索引容易。我们将演示使用图像的高级索引技巧。在深入研究索引之前,我们将安装必......
  • 数组3
    #include<iostream>usingnamespacestd;intmain(){ intLine1[]={1,0,0}; intLine2[]={0,1,0}; intLine3[]={0,0,1}; int*pLine[3]={line1,line2,line3}; cout<<"Matrixtest:"<<endl; for(inti=0;i<3;i++){ for(intj=0;j<3;j++) ......
  • 数组2
    #include<iostream>usingnamespacestd;voidrowSum(inta[][4],intnRow){ for(inti=0;i<nRow;i++){ for(intj=1;j<4;j++) a[i][0]+=a[i][j]; }}intmain(){ inttable[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}}; for(inti=0;i<3;i++){ for(intj=0;j<......
  • 数组1
    #include<iostream>usingnamespacestd;intmain(){ inta[10],b[10]; for(inti=0;i<0;i++){ a[i]=i*2-1; b[10-i-1]=a[i]; } for(constauto&e:a) cout<<e<<""; cout<<endl; for(inti=0;i<10;i++) cout<<b[i]<&......
  • 输入五个int型和五个float型求两个max(数组和重载函数)
    利用数组和函数重载求5个数最大值(分别考虑整数、单精度的情况)。输入格式:分别输入5个int型整数、5个float型实数。输出格式:分别输出5个int型整数的最大值、5个float型实数的最大值。输入样例:在这里给出一组输入。例如:1122666445511.1122.2233.33888.8855.55......
  • 53. 最大子数组和(力扣)
    https://leetcode.cn/problems/maximum-subarray/1.暴力+前缀和classSolution{public:intmaxSubArray(vector<int>&nums){constintN=1e5+10;intsums[N];for(inti=0;i<nums.size();i++)if(i==0)sums[i]=nu......