找了很多资料,很多不全面,要么只有客户端的上传文件资料,要么只有服务器端接收图片的资料。这里就把我整理一致的发出来。
针对:android + springboot开发
android端代码:
public void uploadImage(String localPath,int userId){
File file = new File(localPath);
RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
MultipartBody.Part body =
MultipartBody.Part.createFormData("imageFile",file.getName(),requestFile);
Api.getGankService().uploadImage2IM(body,userId)
.compose(XApi.<ResultModel<String>>getApiTransformer())
.compose(XApi.<ResultModel<String>>getScheduler())
.subscribe(new ApiSubscriber<ResultModel>(){
@Override
protected void onFail(NetError error) {
}
@Override
public void onNext(ResultModel resultModel) {
//上传结果
}
});
}
-----------------------retrofit接口代码---------------------------
@Multipart
@POST("common/images/upload-to-im")
Flowable<ResultModel<String>> uploadImage2IM(@Part MultipartBody.Part imageFile, @Query("userId") int userId);
服务器controller层代码:
@PostMapping("/upload-to-im")
public JsonResult<String> upload2IM(@RequestParam("imageFile") MultipartFile multipartFile,@RequestParam("userId") int userId) {
return photoAlbumService.upload2DefaultAlbum(multipartFile,userId);
}
网友评论