使用Retrofit上传图片的问题

时间:2017-11-13 10:49:29

标签: retrofit2 image-uploading

我正在使用改造在服务器上传图像,图像上传成功,但没有扩展名。 仅表示图像名称正在上传到数据库,扩展名不存在。 所以图像对我来说变得毫无用处。 请提供帮助。 这是我在interface->

中的方法
    @FormUrlEncoded
        @POST("/webservices/AssessorAssetsUpload.php")
        Call<ImageModel> uploadImage(
            @Field("name")String pictype,
            @Field("type")String uploadtype,
            @Field("accesser_id")String asses_id,
            @Field("paper_schedule_id")String paper_id,
            @Field("imagefile")String image
        );


    my activity code->

        String image=imagetoString();

            RetrofitInterface retrofit=ApiClient.getApiClient().create(RetrofitInterface.class);
            Call<ImageModel> call=retrofit.uploadImage(imageName,"image",assessor_id,paperId,image);

            call.enqueue(new Callback<ImageModel>() {
                @Override
                public void onResponse(Call<ImageModel> call, Response<ImageModel> response) {
                    System.out.println(response.body().getStatus()+" "+response.body().getMsg()+" "+response.body().getUrl());

                }
                @Override
                public void onFailure(Call<ImageModel> call, Throwable t) {
                    t.printStackTrace();
                }
            });
        }

    private String imagetoString(){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
    byte[] bytes=stream.toByteArray();
        return Base64.encodeToString(bytes,Base64.DEFAULT);
    }

1 个答案:

答案 0 :(得分:0)

We use Multipart instead of formurlencoded in case of any multimedia object.

     @Multipart
        @POST("/webservices/AssessorAssetsUpload.php")
        Call<ImageModel> uploadImage(
            @Part("name")RequestBody pictype,
            @Part("type")RequestBody uploadtype,
            @Part("accesser_id")RequestBody asses_id,
            @Part("paper_schedule_id")RequestBody paper_id,
            @Part("imagefile")MultipartBody.Part image
        );


    MultipartBody.Part image=imagetoPart();

            RetrofitInterface retrofit=ApiClient.getApiClient().create(RetrofitInterface.class);
            Call<ImageModel> call=retrofit.uploadImage(
                getTextRequest("imageName"),
                getTextRequest("image"),
                getTextRequest("assessor_id"),
                getTextRequest("paperId"),
                image
                );

            call.enqueue(new Callback<ImageModel>() {
                @Override
                public void onResponse(Call<ImageModel> call, Response<ImageModel> response) {
                    System.out.println(response.body().getStatus()+" "+response.body().getMsg()+" "+response.body().getUrl());

                }
                @Override
                public void onFailure(Call<ImageModel> call, Throwable t) {
                    t.printStackTrace();
                }
            });
        }

    private String imagetoPart(){

        Bitmap bitmap = BitmapFactory.decodeFile("Image Path");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
        byte[] bytes=stream.toByteArray();
        RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), bytes);
        MultipartBody.Part part = MultipartBody.Part.createFormData("name", "name", reqFile);

        return part;
    }

 private RequestBody getTextRequest(String body) {

        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), body);

        return requestBody;
    }

我们需要将字段转换为请求正文和图像到多部分正文中,以将其作为多部分请求发送。这将完成你的工作。