Springboot 图片文件和 json同时上传

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

Springboot 图片文件和 json同时上传_第1张图片

服务器端

Springboot 图片文件和 json同时上传_第2张图片

Android端Springboot 图片文件和 json同时上传_第3张图片

 

 

  图片和文字同时上传

  代码直接使用

后端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 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 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 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() {
                 @Override
                 public void onResponse(Call call, Response 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 call, Throwable t) {
                     Log.e("-----------失败--------", t.getMessage()+"");
                 }
             });


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


         }
     });

你可能感兴趣的:(javaWeb,Android,spring,boot,json,android)