【问题标题】:Combine 3 byte arrays组合 3 字节数组
【发布时间】:2014-11-26 10:07:27
【问题描述】:

我试图将 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();

当我将其写入报告时,内容与预期不符。它仅显示第一个 bArray 内容。我在这里出错的地方。

编辑:

getTheReportContent 执行以下操作:

使用 jasper 导出报告。并返回 byteArrList。

 List byteArrList = new ArrayList();
 --------
 exporterXLS.exportReport();
 byteArrList.add(outputStream.toByteArray());

【问题讨论】:

  • getTheReportContent() 实际上做了什么?
  • 你确定getTheReportContent();返回列表吗?
  • 是的..请查看更新后的问题。
  • 检查exportReport/getTheReportContent函数是否第二次和第三次返回null
  • 我问你这个问题是因为我看到一个 List 被分配给 byte[] bArray = getTheReportContent();

标签: java bytearray bytearrayoutputstream


【解决方案1】:

您正在将 List 分配给 byte[] ——这不可能发生。 所以我相信你可能没有粘贴完整的代码。

试过你的代码,结果长度是合适的。您可以检查以下课程。所以我认为问题可能出在接收部分。告诉我

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();
}

【讨论】:

  • 下载文件时显示为 100 mb,但下载后大小为 20 mb。所以我相信,大小正在合并(到标题)。数据没有合并??
  • 不确定。请粘贴一些可以模拟问题的代码
猜你喜欢
  • 2011-11-25
  • 2023-04-02
  • 1970-01-01
  • 2018-12-29
  • 2010-12-16
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多