标题:Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)

1.单张图片的上传

/**         * 上传一张图片         * @param description         * @param imgs         * @return         */        @Multipart        @POST("/upload")        Call<String> uploadImage(@Part("fileName") String description,                                 @Part("file\"; filename=\"image.png\"")RequestBody imgs);


2.多张图片的上传

 /**         * 上传三张图片         * @param description         * @param imgs         * @param imgs1         * @param imgs3         * @return         */        @Multipart        @POST("/upload")        Call<String> uploadImage(@Part("fileName") String description,                                 @Part("file\"; filename=\"image.png\"")RequestBody imgs,                                 @Part("file\"; filename=\"image.png\"")RequestBody imgs1,                                 @Part("file\"; filename=\"image.png\"")RequestBody imgs3);


注意:目前是提供传3张,要想多上传目前我发现的方法就是想要多传一张,就多增加一个参数
@Part("file\"; filename=\"image.png\"")RequestBody imgs,以此类推。

大家看到上面觉得写法很漏,但是用于能力有限,只能想到这样。用Java中的可变参数解决之后,就只能传一张。不能多张。
 @Multipart        @POST("/upload")        Call<String> uploadImage(@Part("fileName") String description,                                 @Part("file\"; filename=\"image.png\"")RequestBody ...imgs);

调用:
 Call<String> call = apiManager.uploadImage( m[0],requestBody1,requestBody2,null);

这样写看上去很是高端,不幸的是只能传一张

3.最后是实现胡过程

3.1创建FileUploadService接口

public interface FileUploadService {    /**  * 上传一张图片  * @param description  * @param imgs  * @return  */  @Multipart    @POST("/upload")    Call<String> uploadImage(@Part("fileName") String description,                             @Part("file\"; filename=\"image.png\"")RequestBody imgs);    /**  * 上传三张图片  * @param description  * @param imgs  * @param imgs1  * @param imgs3  * @return  */  @Multipart    @POST("/upload")    Call<String> uploadImage(@Part("fileName") String description,                             @Part("file\"; filename=\"image.png\"")RequestBody imgs,                             @Part("file\"; filename=\"image.png\"")RequestBody imgs1,                             @Part("file\"; filename=\"image.png\"")RequestBody imgs3);}

3.2创建Retrofit对象

 private static final Retrofit sRetrofit = new Retrofit .Builder()            .baseUrl(ENDPOINT)            .addConverterFactory(GsonConverterFactory.create())//            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作为回调适配器            .build();    private static final FileUploadService apiManager = sRetrofit.create(FileUploadService.class);

3.3调用上传的方法

public static void upload(String path){    String descriptionString = "hello, this is description speaking";    String[] m = new String[2];    m[0]= "share.png";    m[1]=  "Screenshot_20160128-140709.png";    File[]  ssssss= new  File[2];    File file1 = new File("/storage/emulated/0/sc/share.png");    File file = new File("/storage/emulated/0/Pictures/ScreenShots/Screenshot_20160128-140709.png");    ssssss[0]=file;    ssssss[0]=file1;    RequestBody requestBody[] = new RequestBody[3];    RequestBody requestBody1 =            RequestBody.create(MediaType.parse("multipart/form-data"), file);    RequestBody requestBody2 =            RequestBody.create(MediaType.parse("multipart/form-data"), file1);    requestBody[0]=requestBody1;    requestBody[1]=requestBody2;    Call<String> call = apiManager.uploadImage( m[0],requestBody1,requestBody2,null);    call.enqueue(new Callback<String>() {        @Override        public void onResponse(Response<String> response, Retrofit retrofit) {            Log.v("Upload", response.message());            Log.v("Upload", "success");        }        @Override        public void onFailure(Throwable t) {            Log.e("Upload", t.toString());        }    });}

4.服务器段代码:

服务器用的是struts接收:
@Controllerpublic class GetToken extends  ActionSupport {/** *  */private static final long serialVersionUID = 1L;private File[] file;private String[] fileName;public File[] getFile() {return file;}public void setFile(File[] file) {this.file = file;}public String[] getFileName() {return fileName;}public void setFileName(String[] fileName) {this.fileName = fileName;}@Action("/upload")public void login()  {System.out.println("------"+Arrays.toString(file));System.out.println("------"+Arrays.toString(fileName));}}

上传胡结果:
------[\tmp\upload__4348b3f0_1528c3d9735__8000_00000038.tmp,\tmp\upload__4348b3f0_1528c3d9735__8000_00000040.tmp]
------["share.png"]
2016-1-29 16:59:04 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Removing file file \tmp\upload__4348b3f0_1528c3d9735__8000_00000038.tmp
2016-1-29 16:59:04 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Removing file file \tmp\upload__4348b3f0_1528c3d9735__8000_00000040.tmp


总共就这些吧。有问题大家在下面说。QQ1561281670


更多相关文章

  1. Android(安卓)intent.Action 参数值及对应功能介绍(转)
  2. android 将Bitmap转YUV420sp(实例)
  3. C++与java混合编写和C++与OC混合编写 笔记
  4. 【30篇突击 android】源码统计 十七
  5. android BannerView实现自动轮播广告的用法(参考自github)
  6. Android(安卓)Camera之常用接口(人脸追踪源码)简介
  7. android 播放网络或本地gif格式的动态图片
  8. android 开机动画的制作
  9. 【UI交互效果】android UI效果一: coverFlow

随机推荐

  1. Android(安卓)sqlite3 CommandLine
  2. Android闹钟服务AlarmManager
  3. Android 监听网络变化
  4. Android 通过http协议数据交互
  5. Android中ContextMenu的使用
  6. Android Notes 05 - Tasks and Back Stac
  7. android传送照片到FTP服务器
  8. android 抽奖盘动画 自定义View
  9. Android Bitmap与DrawAble与byte[]与Inpu
  10. Android之使用Activity与Fragment通信