我正在尝试通过Spring通过Retrofit将图像上传到S3。因此,基本上这是两个任务。
我知道如何将图片上传到S3,并且效果很好。
我的问题是第一部分。我已经在网上尝试了许多解决方案,但是没有用。这是我的代码->
在Android方面,首先我要获取文件并将其上传到Spring API->
private void UploadFiles() {
File uploadFile = fileArrayList.get(0);
if (uploadFile != null) {
Log.d(TAG, "UploadFiles: File Name is -> " + uploadFile.getName());
RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), uploadFile);
MultipartBody.Part multipartBody = MultipartBody.Part.createFormData("image", uploadFile.getName(), requestFile);
cropImageRequest.setCropId(uploadFile.getParent());
cropImageRequest.setCropImage(multipartBody);
FarmerResponse farmerResponse = UserAuth.getInstance(this).getCurrentUser();
Api.uploadCropImage(farmerResponse.getAuthToken(), farmerResponse.getFarmer().getId(), cropImageRequest, new Callback<BasicResponse>() {
@Override
public void onResponse(Call<BasicResponse> call, Response<BasicResponse> response) {
Log.d(TAG, "onResponse: Success : " + response.toString());
}
@Override
public void onFailure(Call<BasicResponse> call, Throwable t) {
Log.d(TAG, "onResponse: Failure : " + t.getMessage());
}
});
}
}
我创建了cropImageRequest.java,它基本上是请求正文的POJO类。这是我的POJO类,其中包含Image和ImageId。 ->
import okhttp3.MultipartBody;
public class CropImageRequest {
private String CropId;
private MultipartBody.Part CropImage;
public CropImageRequest() {
}
public void setCropId(String cropId) {
CropId = cropId;
}
public void setCropImage(MultipartBody.Part cropImage) {
CropImage = cropImage;
}
public CropImageRequest(String cropId, MultipartBody.Part cropImage) {
CropId = cropId;
CropImage = cropImage;
}
public String getCropId() {
return CropId;
}
public MultipartBody.Part getCropImage() {
return CropImage;
}
}
这是我的API声明,位于API类内。 ->
public interface UploadCropImageApi {
@POST(UPLOAD_FILE_TO_AWS_URL)
Call<BasicResponse> uploadCropImage(
@Header(MOBILE_AUTH) String authToken,
@Header(FARMER_ID) String farmerId,
@Body CropImageRequest cropImageRequest);
}
public static void uploadCropImage(String authToken, String farmerId, CropImageRequest cropImageRequest, Callback<BasicResponse> callback) {
UploadCropImageApi uploadCropImageApi = retrofit.create(UploadCropImageApi.class);
Call<BasicResponse> call = uploadCropImageApi.uploadCropImage(authToken, farmerId, cropImageRequest);
call.enqueue(callback);
}
在Spring方面,我开发了一个控制器,该控制器处理API请求并将通过的图像上传到S3->
@RequestMapping(value = "/UploadCropImage",method = RequestMethod.POST,produces = "application/json")
@ResponseBody
public String UploadImage(@RequestBody CropImageRequest cropImageRequest,HttpServletRequest request) {
uuid = new UUIDUtils();
fileUtils = new FileUtils();
try {
String KeyName = uuid.GenerateUUID(fileUtils.convert(cropImageRequest.getCropImage()));
System.out.println(KeyName);
return mAmazonClient.uploadImage(KeyName,cropImageRequest.getCropImage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
这是我的Convert函数,它将MultiPart File转换为File。
public String convert(MultipartFile file) throws IOException {
if(file!=null){
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile.getName();
}
else{
System.out.println("Supplied File is null");
return null;
}
}
问题是在Spring端,我收到了空指针异常的条件检查。这意味着文件未上传。在Android Side上,我已经通过打印出文件名进行了验证,并且正在打印正确的文件名。顺便说一句,我正在上传.WebP格式的文件,我不认为这会造成任何问题。感谢您的提前帮助。
答案 0 :(得分:1)
您正在使用多部分/表单数据发送信息 所以尝试使用
@ModelAttribute
代替
@RequestBody
方法签名将为
public String UploadImage(@ModelAttribute CropImageRequest cropImageRequest,HttpServletRequest request)