有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用二进制文件更新上传图像?

我正在开发一个安卓应用程序,它可以发布一些用户输入的带有用户签名的数据(使用signaturepad捕获签名并将其转换为bmp文件)

现在,在发布全部数据之前,我需要请求服务器上传我的画布签名。所以服务器会回复两个url(参考图1),分别是put url和get url。现在我必须使用put url上传签名图像文件,整个过程通过postman成功完成,但我无法在安卓应用程序上实现。Put URl的主体是二进制的,我选择了一些随机图像文件。 有关参考信息,请参见所附的屏幕截图Pre Requesting the server for uploading imagePre Request body{a3}{a4}{a5}

Retrofit retrofit_image_upload=new Retrofit.Builder()
                                        .baseUrl(Aws_url) // base url retrive using Uri class using uri.getAuthority and concat with "https:/"
                                        .addConverterFactory(GsonConverterFactory.create())
                                        .build();
                ByteArrayOutputStream stream=new ByteArrayOutputStream();
                signature.compress(Bitmap.CompressFormat.PNG,100,stream);
                byte[] byteArray=stream.toByteArray();
                final RequestBody requestBody=RequestBody.create(MediaType.parse("application/octet-stream"),byteArray);

现在调用api并传递数据

                                Call<Object> image_upload_call= null;
                                try {
                                    image_upload_call = final_upload.aws_upload(path,auth,requestBody.contentLength(),x_amz_acl,AWSAccessKeyId,Expires,Signature_aws,requestBody);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

上传图像的接口

    @PUT("{Put_path}")
    Call<Object> aws_upload(@Path("Put_path") String path,
                            @Header("auth") String auth,
                            @Header("Content-Length") long length,
                            @Query("x-amz-acl") String x_amz,
                            @Query("AWSAccessKeyId") String awskey,
                            @Query("Expires") String Expires,
                            @Query("Signature") String Awssignature,
                            @Body RequestBody image);
}

当我使用此代码时,安装服务器不接受来自安卓应用程序的文件,并且响应为错误代码403,当我尝试从postman软件上载图像文件时,它工作正常。那么,我该如何在安卓应用程序中实现这一点呢。 服务器出错

    <Code>SignatureDoesNotMatch</Code>
    <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

共 (0) 个答案