Retrofit2.0进行图片上传(后端Spring MVC)

你该绕过的坑

Android端

  1. 用版本不低于2.0.1的库,不然在进行上传时会报类型转换错误

  2. 转换工厂库版本也不应低于2.0.1的


build.gradle依赖

compile ‘com.squareup.retrofit2:retrofit:2.0.1’
compile ‘com.squareup.retrofit2:converter-gson:2.0.1’


Retrofit初始化

Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                //基础地址,这里我以本地测试进行
                .baseUrl("http://192.168.30.149:8080")
                .build();

接口编写

  • 需要上传其他参数,可在方法里使用@Part进行添加 。实际开发中,我们上传图片需要与后台用户表进行关联,建议讲token放于请求头。

  • 当上传因为其他原因不成功时,文件名在后台配置的文件夹里会以多一个引号结尾

  • 下面的‘pic’为与后台约定的文件参数key
public interface FileUploadService {
  /**
   * 上传一张图片
   * @param description
   * @param imgs
   * @return
   */
  @Multipart
  @POST("/app/person/uploadpic.htm")
  Call<Result> uploadImage(@Part("filename") String description,
                                 @Part("pic\"; 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);
 }

使用

        File file = new File("/mnt/sdcard/Download/29381f30e924b899cf2bfd036b061d950a7bf6f2.jpg");

//用RequestBody包裹文件,application/octet-stream为文件类型,这里为二进制流,不知道文件类型时所用
        RequestBody requestBody =
                RequestBody.create(MediaType.parse("application/octet-stream"), file);

        FileUploadService apiManager = retrofit.create(FileUploadService.class);
        Call<Result> call = apiManager.uploadImage("img",requestBody);

        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                    Log.d("=======",response.body().getUrl());

            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                    Log.d("======",t.toString());
            }
        });

Spring MVC


文件上传配置mod_spring_view.xml

<!-- 文件上传 --> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
        p:defaultEncoding="UTF-8"
        p:resolveLazily="true"
        p:maxUploadSize="52428800"
        p:uploadTempDir="upload/temp"/>

Controller层

    @RequestMapping(value = "uploadPic.htm")
    public String uploadPic(Model model, MultipartFile pic){
        Result result = new Result();
        //...此处进行存储,每个人写法不一样
        return "jsonview";
    }
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值