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

来源:互联网 发布:删除淘宝评价怎么删 编辑:程序博客网 时间:2024/05/23 11:53

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