java中如何将一个zip文件转为byte数组

OutputStream imgOutputStream = response.getOutputStream();
output = new BufferedOutputStream(imgOutputStream);// 输出缓冲流
byte data1[] = new byte[1024*1024*10];// 缓冲字节数 8K
BufferedInputStream input1 = null;
File file=new File(Timing+"A.zip");
FileInputStream fos=new FileInputStream(file);
input = new BufferedInputStream(fos);// 输入缓冲流
int length = input1.read(data1);
for (int length1 = 0; (length1 = input1.read(data1)) > 0;) {
output.write(data1, 0, length1);
}
output.flush();
output.close();
这个代码里,那个file文件就是A.ZIP,可是传过去是空的,我该怎么样将zip文件转为byte数组呢

读入时使用ByteArrayOutputStream缓存,然后转成byte[]

import java.io.*;
public class ReadFileBytes{
    public static void main(String arg[]) throws FileNotFoundException, IOException{
        Integer g[] = new Integer [60];
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        FileInputStream      fin  = new FileInputStream("ReadFileBytes.java");
        
        int read;
        byte[] bytes=new byte[1024];
        while((read = fin.read(bytes)) >0){
            out.write(bytes, 0, read); 
        }
        fin.close();
        
        bytes = out.toByteArray(); // 这就是全部的字节数组了。
        out.close();
        
        System.out.println(bytes.length);
    }
}

 

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2017-12-16
input1.read(data1);
这一句执行好data1 就是文件的byte数组了!!!
String str = new String(data1);//转换成string
下面不要了
for (int length1 = 0; (length1 = input1.read(data1)) > 0;) { output.write(data1, 0, length1); }本回答被提问者和网友采纳
第2个回答  推荐于2018-05-18
OutputStream imgOutputStream = response.getOutputStream();
output = new BufferedOutputStream(imgOutputStream);// 输出缓冲流
byte data1[] = new byte[1024*1024*10];// 缓冲字节数 8K
BufferedInputStream input1 = null;
File file=new File(Timing+"A.zip");
FileInputStream fos=new FileInputStream(file);
input = new BufferedInputStream(fos);// 输入缓冲流
int length = input1.read(data1);
相似回答