Android -Raterofit-用预先签名的URL上传到AWS S3的文件已损坏



在我的应用程序中,我需要将图像直接上传到AWS S3。为此,我的服务器生成了预先签名的URL和移动客户端使用该URL将文件放置。尽管在上传呼叫中获得200个,但该文件未正确上传,即损坏,永远不会加载。以下是用于将文件上传到S3的代码。

public static interface FileUploadService {
    @PUT("/")
    void upload(@Body() RequestBody body,
                Callback<Object> callback);
}

ServiceGenerator.getUploadService(url).upload(
                    RequestBody.create(MediaType.parse("image/jpeg"), image),
                    new Callback<Object>() { });

我正在使用改造1.8。

您好,如果有帮助,请尝试此尝试,但我使用了Retrofit 2,

gradle

 compile 'com.squareup.retrofit2:retrofit:2.0.2'

接口

public static interface FileUploadService {
   @Multipart
    @POST(HttpConstants.FILEUPLOADJSON1)
    Call<Result> uploadImage(@Part MultipartBody.Part file, @Part("stdID") int stdID);
}

呼叫

raytaapi服务= raytaserviceclass.getapiservice((;

    File file = new File(imagePath);
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

    MultipartBody.Part body =
            MultipartBody.Part.createFormData("uploaded_file", file.getName(), requestFile);
    Call<Result> resultCall=service.uploadImage(body,INT_STDID);
    final Result[] result = {new Result()};
    resultCall.enqueue(new Callback<Result>() {
        @Override
        public void onResponse(Call<Result> call, Response<Result> response) {
            Log.v("@@@WWE","Respnse");
            }
        }
        @Override
        public void onFailure(Call<Result> call, Throwable t) {
            Log.v("@@@WWE","Failure ");
            Log.v("@@@WWE","MEssage "+t.getMessage());
        }
    });

有关完整的示例,请访问https://github.com/pratikvyas1991/networkingexample-master

固定如下;

使用RequestBody时,Content-Length标头无法携带正确的信息,我也无法覆盖它。因此,我使用TypedInput而不是RequestBody。我的服务界面看起来如下。

public static interface FileUploadService {
    @PUT("/")
    void upload(@Body TypedInput body, Callback<Object> callback);
}

和图像被上传为;

ServiceGenerator.getUploadService(url).upload(new TypedFile("image/*", file), 
                                              new Callback<Object>() { });

最新更新