需求: 移动端经常遇到以下需求
服务器端
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()+""); // } // }); } });