Springboot 图片文件和 json同时上传

需求: 移动端经常遇到以下需求

服务器端

Android端

 

 

  图片和文字同时上传

  代码直接使用

后端api代码

@PostMapping("/uploadVideos")
public Result insertVideos(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("material") TbVideo tbVideo) {

}

 @PostMapping("/uploadVideos")
    public Result insertVideos(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("material") TbVideo tbVideo) {
        tbVideo.setCreatetime(new Date());

        try {
            /**
             * 添加视频
             */


            if (file != null) {
                List<String> upload = fileServer.uploadVideo(file);
                if (upload != null) {
                    String video_url = address + "/" + bucketName + "/" + upload.get(0);
                    tbVideo.setVideopath(video_url);
                }
            }

            /**
             * 添加
             *
             */


            SysUser userInfo = SecurityUtil.getUserInfo();
            if (userInfo != null) {
                tbVideo.setUserid(userInfo.getId());
                tbVideo.setUsername(userInfo.getUsername());
                tbVideo.setAvatar(userInfo.getAvatar());
                tbVideo.setType(tbVideo.getType());
                tbVideo.setViewcount(tbVideo.getViewcount());
                tbVideo.setCreatetime(new Date());
            }


             tabVideoServers.insertVideo(tbVideo);

            return new Result(true, 200, "添加成功", tbVideo);

        } catch (Exception e) {
            return Result.fail("添加失败");
        }

    }

Android端代码

api接口

public interface Api {
    //http://192.168.1.4:80/uploadVideos
    @Multipart
    @POST("/uploadVideos")
    Call<ResponseBody> postFiles(@Part MultipartBody.Part file, @Part("material") RequestBody description);

 
}
  btn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {

             OkHttpClient okHttpClient = new OkHttpClient.Builder()
                     .connectTimeout(6000, TimeUnit.SECONDS)
                     .writeTimeout(6000, TimeUnit.SECONDS)
                     .readTimeout(6000, TimeUnit.SECONDS)
                     .build();

             Retrofit build = new Retrofit.Builder()
                     .baseUrl("http://192.168.1.4:80")
                     .client(okHttpClient)
                     .addConverterFactory(GsonConverterFactory.create()).build();

             Api api = build.create(Api.class);






             Map<String, String> map = new HashMap<>();
             map.put("title", "小牧");
             map.put("content", "23");
             map.put("videopath", "23");
             map.put("userid", "12");
             map.put("username", "23");
             map.put("avatar", "23");
             String data = new Gson().toJson(map);
             System.out.print(data);

             MediaType types = MediaType.parse("application/json; charset=UTF-8");
             RequestBody requestBody = RequestBody.create(types, data);

             File file = new File(locals);
             RequestBody fileRQ= RequestBody.create(MediaType.parse("image/jpeg"),  file);
             MultipartBody.Part part = MultipartBody.Part.createFormData("file",  file.getName(), fileRQ);
             api.postFiles(part,requestBody).enqueue(new Callback<ResponseBody>() {
                 @Override
                 public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                     ResponseBody body = response.body();

                     try {
                         String string = body.string();
                         Log.e("--------------sss-----", string  );
                     } catch (Exception e) {

                         Log.e("--------------sss-----",  e.getMessage() );
                     }
//

                 }

                 @Override
                 public void onFailure(Call<ResponseBody> call, Throwable t) {
                     Log.e("-----------失败--------", t.getMessage()+"");
                 }
             });


//             api.postuserAvater(part).enqueue(new Callback<Request>() {
//                 @Override
//                 public void onResponse(Call<Request> call, Response<Request> response) {
//                     Request body = response.body();
//                     Request.data data = body.getData();
//
//
//                     Log.e("-------------------", body.toString()+"--"+data.toString());
//
//                 }
//
//                 @Override
//                 public void onFailure(Call<Request> call, Throwable t) {
//                     Log.e("-----------失败--------", t.getMessage()+"");
//                 }
//             });


         }
     });

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值