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
- Adnroid Retrifit2.0 多图片上传 服务端Java
- android Retrifit2.0+OKHttp文件上传
- Java IOS客户端上传多张图片到服务端
- java 两种上传文件(图片)方式(app通过流上传)+服务端获取方式(多图上传,普通文本上传,图片压缩)
- Winform客户端到Java服务端的图片展示和上传
- Android头像上传和java服务端接收图片。
- 微信小程序开发之图片上传+Java服务端接收
- Java 服务端接收多图上传
- TCP 上传图片到服务端
- TCP协议多个用户同时上传图片到服务端
- Adnroid 图片裁剪
- Adnroid多媒体---图片
- 客户端和服务端验证上传图片限制
- Android如何压缩图片上传服务端
- post请求 上传图片到服务端
- android端上传图片到服务端
- javaseday31(图片上传服务端 客户端单线程)
- 【java基础:net】基于多线程的TCP图片上传客户端与服务端Demonstration
- 机器 · 人 | 可重复的事情,让机器去做
- 文章标题
- Codeforces Round #363 (Div. 1) B. Fix a Tree 拆环+合并树
- iOS开发的22个奇葩技巧
- Codeforces 155A I_love_%username%
- Adnroid Retrifit2.0 多图片上传 服务端Java
- 百度地图api的SHA1
- 监督学习应用.正规方程组
- IP数据报的分片和组装过程
- centos安装python mvc框架django1.9
- PrinterDemo
- Codeforces 155B Combination
- 面试题总结-Java基础
- PopupWindow注意事项