Adnroid Retrifit2.0 多图片上传 服务端Java

Android端关于retrifit 2.0 多图片上传资料很多,但是相对应的webservice只找到几篇php的文章。初始,服务端接收了各种形式的参数都是null,通过抓接口才明白原来android和javaWeb传数据都是按web规范进行处理,android传递的是map,name全部相同,filename做区别,服务端根据name以数组形式接收参数。

抓包图:

1.

2.

3.


Android核心代码:

/**多图片上传
 */
public void uploadImgList(Callback callback, Map<String, RequestBody> imgs){
    ApiStore.uploadImgList con = retrofit.create(ApiStore.uploadImgList.class);
Call<ImgBean> call = con.getInfo(imgs);
call.enqueue(callback);

 File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/", "icon.png");
                File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/", "icon1.png");
                File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/", "icon2.png");
                
                Map<String,RequestBody> imgs = new HashMap<>();
                RequestBody requestImg = RequestBody.create(MediaType.parse("image/png"),file);
                RequestBody requestImg1= RequestBody.create(MediaType.parse("image/png"),file1);
                RequestBody requestImg2= RequestBody.create(MediaType.parse("image/png"),file2);
                imgs.put("imgs\"; filename=\"icon.png",requestImg);
                imgs.put("imgs\"; filename=\"icon1.png",requestImg1);
                imgs.put("imgs\"; filename=\"icon2.png",requestImg2);

HttpClient.getInstance().uploadImgList(new Callback() {
                    @Override
                    public void onResponse(Call call, Response response) {
                        ImgBean data = (ImgBean) response.body();
                        if (data != null) {
                            Snackbar.make(mButton2, "" + data.toString(), Snackbar.LENGTH_SHORT).show();
                            Log.d("成功", data.toString());
                        }
                    }

                    @Override
                    public void onFailure(Call call, Throwable t) {
                        Snackbar.make(mButton2, "" + t, Snackbar.LENGTH_SHORT).show();
                        Log.d("失败", "");
                    }
                },imgs);
               


Web核心代码:

@RequestMapping(value = "uploadImage.do")
    public void uploadListImage(
            @RequestParam("imgs") CommonsMultipartFile imgs[],HttpServletRequest request,
            HttpServletResponse response, ModelMap model) throws IOException {
        List<Map<String, String>> succeedList = new ArrayList<Map<String, String>>();
        JSONObject bean = new JSONObject();
            for (CommonsMultipartFile file : imgs) {
                if (!file.isEmpty()) {
                    String path = request.getSession().getServletContext()
                            .getRealPath("/")
                            + MobileContainer.IMAGE_PATH;// 路径
                    String fileName = category + UtilTime.getTime()
                            + UtilTime.getRandom() + ".png";// 文件名

                    boolean isSucceed = MyFileUtil.uploadFile(path, fileName,
                            file);

                    Map<String, String> map = new HashMap<String, String>();
                    if (isSucceed) {
                        int num = imgService.insert(requestData);
                        String imgPath = UtilTime.getURL(request, fileName);
                        map.put("isSu", "y");
                        map.put("url", imgPath);
                        map.put("id", num + "");
                        map.put("name", file.getFileItem().getFieldName());
                    } else {
                        map.put("isSu", "n");
                    }
                    succeedList.add(map);
                    bean.put("code","200");
                    bean.put("datas",JSONArray.fromObject(succeedList)
                            .toString());
                }
        }
        ResponseUtils.renderJson(response, bean.toString());
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值