2016-10-12 4 views
-2

파일 배열로 업로드되는 멀티 파트 엔티티 파일 업로드. 아래 오류 코드를 언급 했으므로이 문제를 해결하는 데 도움이됩니다. 예외 java.lang.ArrayIndexOutOfBoundsException : 길이 = 2; 색인 = 2. 미리 감사드립니다.멀티 파트 엔티티 파일 업로드 java.lang.ArrayIndexOutOfBoundsException

코드 :

try{ 
     int i = 0; 
     HttpEntity httpEntity = null; 
     HttpResponse httpResponse = null; 
     HttpClient httpClient = new DefaultHttpClient(); 
     int selectedImgLength = selectedItems.size(); 
     File[] mfile = new File[selectedImgLength]; 
     for(i = 0; i<selectedImgLength;i++){ 
      //mfile[i]= selectedItems.get(i);// Error InCompatiable type 
      mfile[i] = new File(selectedItems.get(i)); 
     } 
     HttpPost httpPost = new HttpPost(url); 
     MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); 
     entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
     entityBuilder.addPart("userfile[" + i + "]", new FileBody(mfile[i])); 
     HttpEntity entity = entityBuilder.build(); 
     httpPost.setEntity(entity); 

     httpResponse = httpClient.execute(httpPost); 
     httpEntity = httpResponse.getEntity(); 
     response = EntityUtils.toString(httpEntity); 
     System.out.println("POST IMAGE Response"+response); 
    }catch(Exception e){e.printStackTrace();} 
+0

나는 왜 내 질문에 투표를하는지 알 수 있습니다. 나에게 의견을 주시면 스택 질문을 개선하는 데 도움이 될 것입니다. –

답변

1

당신은 당신이 이미 그러므로 당신이 ArrayIndexOutOfBoundsException

을 얻을 것이다, 루프 i에 대한 selectedImgLength에 크기가 동일되고있다 종료 한

entityBuilder.addPart("userfile[" + i + "]", new FileBody(mfile[i])); 

할 때 for 루프 내의 entityBuilder에 파일을 추가하도록 변경하십시오.

+0

고마워 형, 큰 실수를 저지른 것을 유감스럽게 생각합니다. 귀하의 안내에 감사드립니다. 이제 나는 그것을 고쳤다. –

관련 문제