Arrays 异常:null类:Class java.lang.ArrayIndexOutOfBoundsException

Arrays 异常:null类:Class java.lang.ArrayIndexOutOfBoundsException,arrays,blackberry,byte,concatenation,indexoutofboundsexception,Arrays,Blackberry,Byte,Concatenation,Indexoutofboundsexception,我在下面的代码行中获取ArrayIndexOutOfBoundsException: String boundaryMessage=getBoundaryMessage(边界、文件字段、文件名、文件类型); 字符串endBoundary=“\r\n-->+boundary+”-->\r\n”; byte[]temp=新字节[boundaryMessage.getBytes().length+fileBytes.length+endBoundary.getBytes().length]; temp

我在下面的代码行中获取ArrayIndexOutOfBoundsException:

String boundaryMessage=getBoundaryMessage(边界、文件字段、文件名、文件类型);
字符串endBoundary=“\r\n-->+boundary+”-->\r\n”;
byte[]temp=新字节[boundaryMessage.getBytes().length+fileBytes.length+endBoundary.getBytes().length];
temp=boundaryMessage.getBytes();
试一试{
System.arraycopy(fileBytes,0,temp,temp.length,fileBytes.length);//此处引发异常
System.arraycopy(endBoundary.getBytes(),0,temp,temp.length,endBoundary.getBytes().length);
}
捕获(例外e){
System.out.println(“==异常:+e.getMessage()+”类:+e.getClass());
}

有人能告诉我哪里错了吗。谢谢。

当您选择
temp.length
作为dst_position参数时,您使用第四个参数来阵列复制不正确。这意味着您希望在
temp
数组结束后开始目标。第一次尝试写入超过数组末尾时,会产生一个
ArrayIndexOutOfBoundsException
,如您所见。检查:

如果我理解您想要正确执行的操作,您应该能够通过将temp.length更改为0来修复它,这意味着您希望将fileBytes复制到temp的开头:

System.arraycopy(fileBytes, 0, temp, 0, fileBytes.length);

但我真正想要实现的是将fileBytes数组复制到我的temp数组中。这就是我为什么这样做的原因。我已经在开头指定了数组的空格。还有什么要做?
System.arraycopy(fileBytes, 0, temp, temp.length, fileBytes.length);
System.arraycopy(fileBytes, 0, temp, 0, fileBytes.length);