0

나는 아래의 코드와 같이 행의 경우 ArrayIndexOutOfBoundsException 받고 있어요 : 내가 틀렸다 곳예외 : 널 (null) 클래스 : 클래스 java.lang.ArrayIndexOutOfBoundsException

String boundaryMessage = getBoundaryMessage(boundary, fileField, fileName, fileType); 

String endBoundary = "\r\n--" + boundary + "--\r\n"; 
byte[] temp = new byte[boundaryMessage.getBytes().length+fileBytes.length+endBoundary.getBytes().length];  

temp = boundaryMessage.getBytes(); 
try { 
    System.arraycopy(fileBytes, 0, temp, temp.length, fileBytes.length); //exception thrown here    
    System.arraycopy(endBoundary.getBytes(), 0, temp, temp.length, endBoundary.getBytes().length); 
} 
catch(Exception e){ 
    System.out.println("====Exception: "+e.getMessage()+" Class: "+e.getClass()); 
} 

누군가가 날 지점 수 있습니다. 감사.

답변

1

temp.length을 dst_position 인수로 선택하면 arraycopy의 네 번째 인수가 잘못 사용됩니다. 즉, 배열이 temp 배열 끝을 지나서 시작되기를 원합니다. 배열의 끝을 지나서 처음으로 쓰려고하면보고있는 것처럼 ArrayIndexOutOfBoundsException이됩니다. 대상 배열의 지정된 위치로 지정된 위치에서 시작하여, 지정된 소스 배열에서

 
public static void arraycopy(Object src, 
          int src_position, 
          Object dst, 
          int dst_position, 
          int length) 

복사 배열을 다음 documentation을 확인합니다. 배열 요소의 서브 순서는, src에 의해 참조되는 전송 원 배열로부터, dst에 의해 참조되는 전송처 배열에 카피됩니다. 복사 된 구성 요소의 수는 length 인수와 같습니다. 소스 배열의 srcOffset ~ srcOffset + length-1 위치의 구성 요소는 대상 배열의 dstOffset ~ dstOffset + length-1 위치에 각각 복사됩니다.

편집 1월 22일

귀하의 문제가있는 줄은 다음과 같습니다 : 당신이 올바르게 수행 할 작업을 이해한다면

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

, 당신은 온도를 변경하여 문제를 해결 할 수 있어야한다. 길이를 0으로 설정하면 fileBytes를 임시의 시작 부분에 복사합니다.

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

정말 달성하고 싶습니다 fileBytes 배열을 내 임시 배열에 복사하는 것입니다. 그래서 내가 이렇게 해왔다. 나는 처음부터 배열의 공간을 이미 지정했다. 무엇을 더해야할까요? –

관련 문제