Java 组合3字节数组

Java 组合3字节数组,java,bytearray,bytearrayoutputstream,Java,Bytearray,Bytearrayoutputstream,我试图将3字节数组组合成一个数组来生成报告 byte[] bArray=null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream( ); for(int i=0;i<3;i++) { //Other stuff bArray = getTheReportContent(); //The return type of this method is List<byte[]> outputStream

我试图将3字节数组组合成一个数组来生成报告

byte[] bArray=null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );

for(int i=0;i<3;i++)
{
//Other stuff
bArray = getTheReportContent(); //The return type of this method is List<byte[]> 
outputStream.write(bArray);
} 

byte bArrayCombined[] = outputStream.toByteArray( );  //Checked the count. bArrayCombined.length=sum of all the 3 bArray

response.setContentLength((int) bArrayCombined.length);
outStream.write(bArrayCombined, 0, bArrayCombined.length);
outStream.flush();

您正在将列表分配给字节[]-这是不可能发生的。 所以我相信你可能没有粘贴完整的代码

尝试了你的代码,结果长度是正确的。你可以在下面的课程中进行检查。所以我认为问题可能在接收部分。让我知道

public static void main(String[] args) {
    byte[] bArray=null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );

    for(int i=0;i<3;i++)
    {
        try {
            //Other stuff
            bArray = getTheReportContent(); //The return type of this method is List<byte[]> 
            outputStream.write(bArray);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

    byte bArrayCombined[] = outputStream.toByteArray( );  //Checked the count. bArrayCombined.length=sum of all the 3 bArray
    System.out.println("Size is " + bArrayCombined.length);

    ByteArrayOutputStream outStream = new ByteArrayOutputStream( );
    outStream.write(bArrayCombined, 0, bArrayCombined.length);

    System.out.println("new outStream size is " + outStream.toByteArray().length);

}

private static byte[] getTheReportContent() {
    return "123".getBytes();
}

getTheReportContent实际上做什么?你确定getTheReportContent;返回列表?是..请查看更新后的问题。检查exportReport/getTheReportContent函数第二次和第三次是否返回null我问过你这个问题,因为我看到列表被分配给字节[]bArray=getTheReportContent;下载文件时显示为100 mb,但下载后大小为20 mb。所以我相信,这个尺寸正被合并到页眉上。数据未合并??不确定。请粘贴一些可以模拟问题的代码
public static void main(String[] args) {
    byte[] bArray=null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );

    for(int i=0;i<3;i++)
    {
        try {
            //Other stuff
            bArray = getTheReportContent(); //The return type of this method is List<byte[]> 
            outputStream.write(bArray);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

    byte bArrayCombined[] = outputStream.toByteArray( );  //Checked the count. bArrayCombined.length=sum of all the 3 bArray
    System.out.println("Size is " + bArrayCombined.length);

    ByteArrayOutputStream outStream = new ByteArrayOutputStream( );
    outStream.write(bArrayCombined, 0, bArrayCombined.length);

    System.out.println("new outStream size is " + outStream.toByteArray().length);

}

private static byte[] getTheReportContent() {
    return "123".getBytes();
}