2016-10-12 66 views
-2

多部分实体文件上传文件数组。 我已经提到了下面的错误代码,请帮我解决这个问题。异常java.lang.ArrayIndexOutOfBoundsException:length = 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

当你做

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

您已经退出for循环和i已成为等于在尺寸上selectedImgLength,因此,你会得到一个ArrayIndexOutOfBoundsException

尝试更改以便将文件添加到for循环中的entityBuilder

+0

感谢兄弟,对不起,我犯了一个大错误。感谢您的指导。现在我修正了它。 –