问题描述
多部分实体文件上传,带有文件数组。
我在下面提到了错误代码,请帮助我解决此问题。异常java.lang.ArrayIndexOutOfBoundsException:length = 2;索引= 2。
预先感谢。
Multipart entity file uploading with array of files.I have mentioned the error code below, please help me resolve this issue. Exception java.lang.ArrayIndexOutOfBoundsException: length=2; index=2.Thanks in Advance.
代码:
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();}
推荐答案
执行时
entityBuilder.addPart( "userfile[" + i + "]", new FileBody(mfile[i]));
您已经退出了for循环和 i
的大小已等于 selectedImgLength
,因此您将得到 ArrayIndexOutOfBoundsException
you have already exited the for loop and i
has become equals in size to selectedImgLength
, therefore you will get a ArrayIndexOutOfBoundsException
尝试更改,以便将文件添加到for循环内的 entityBuilder
中。
try changing so that adding the file to the entityBuilder
within the for loop.
这篇关于多部分实体文件上传java.lang.ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!