项目中需要用到类似朋友圈心情的多图上传功能,一直做了很久都没有能把图片上传成功,后来一朋友说用xUtils框架,妈的,还真的几句代码就可以了,哈哈,话不多说,直接上代码
一、需要用到的jar包:
二、上传方法:
private void uploadFile() { RequestParams params = new RequestParams(); params.addBodyParameter( "picture1", new File( "/storage/sdcard1/sina/weibo/weibo/img-2d9ccb2c2900a884b3e6535bb3727cef.jpg")); params.addBodyParameter( "picture2", new File( "/storage/sdcard1/sina/weibo/weibo/img-4d15ac4a5f36404f2ecddc10cd278410.jpg")); HttpUtils http = new HttpUtils(); http.send(HttpRequest.HttpMethod.POST, actionUrl, params, new RequestCallBack<String>() { @Override public void onStart() { Log.e("start", "start"); } @Override public void onLoading(long total, long current, boolean isUploading) { if (isUploading) { Toast.makeText(MainActivity.this, "upload: " + current + "/" + total, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "reply: " + current + "/" + total, Toast.LENGTH_SHORT).show(); } } @Override public void onSuccess(ResponseInfo<String> responseInfo) { Toast.makeText(MainActivity.this, "reply: " + responseInfo.result, Toast.LENGTH_SHORT).show(); Log.e("responseinfo", responseInfo.result); } @Override public void onFailure(HttpException error, String msg) { Toast.makeText(MainActivity.this, error.getExceptionCode() + ":" + msg, Toast.LENGTH_SHORT).show(); Log.e("error", msg); } }); }
三、运行结果如下: