【问题标题】:Multipart entity file uploading java.lang.ArrayIndexOutOfBoundsException多部分实体文件上传 java.lang.ArrayIndexOutOfBoundsException
【发布时间】:2017-02-20 19:31:50
【问题描述】:

带有文件数组的多部分实体文件上传。 我在下面提到了错误代码,请帮我解决这个问题。异常 java.lang.ArrayIndexOutOfBoundsException: length=2;指数=2。 提前致谢。

代码:

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

【问题讨论】:

  • 我可以知道为什么我的问题被否决了。请给我评论,这将有助于改进堆栈中的提问。

标签: java android asyncfileupload multifile-uploader


【解决方案1】:

当你这样做时

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

您已经退出了 for 循环,并且 i 的大小已与 selectedImgLength 相等,因此您将得到一个 ArrayIndexOutOfBoundsException

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

【讨论】:

  • 谢谢兄弟,对不起,我犯了一个大错误。感谢您的指导。现在我修好了。
猜你喜欢
  • 2021-02-11
  • 1970-01-01
  • 2014-07-12
  • 2015-06-26
  • 2018-10-15
  • 2014-10-31
  • 2017-11-05
  • 2015-04-10
  • 1970-01-01
相关资源
最近更新 更多