通过改造2上传图片

时间:2017-09-07 05:51:47

标签: android retrofit2 image-uploading

接口

 select * from
(select row_number() over ( order by (select null) ) rn, 
     (select distinct  test1,test2,test3
from table1
where table1.test1= 1
EXCEPT
select distinct  test1,test2,test3
from table2
where table2.test1= 1)
 ) 
  where rn between 0 and 100

致电api:

@Multipart
@POST("emp/passportupload")
Single<ApiResponse> uploadPassportImage(@Query("passportnumber") String passportNumber, @Part MultipartBody.Part file);

我使用此方法将图像上传到服务器,但服务器无法将其验证为图像,因此给我一个响应,如

“提供的文件不是有效图片。请提供PNG / JPG文件”

但是,我已经通过邮递员上传了相同的图像文件并且成功了。这是请求:( N.B:passportnumber是一个参数,而不是表格数据) enter image description here

4 个答案:

答案 0 :(得分:2)

@Multipart
@POST("changeCompanyLogo")
Call<ChangeLogoResponse> changeCompanyLogo(@Part MultipartBody.Part image, @Part("JSON") RequestBody name);

在服务中编写此代码

ChangeLogoAPI service = ServiceHandler.getClient().create(ChangeLogoAPI.class);
        File file = new File(intent.getStringExtra("imagePath"));
        RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("companyLogo", file.getName(), reqFile);
        RequestBody name = RequestBody.create(MediaType.parse("text/plain"), new Gson().toJson(new ChangeLogoParams()));
        Call<ChangeLogoResponse> call = service.changeCompanyLogo(body, name);
        call.enqueue(new Callback<ChangeLogoResponse>() {
            @Override
            public void onResponse(Call<ChangeLogoResponse> call, Response<ChangeLogoResponse> response) {
                Log.d(TAG, "response: " + response.isSuccessful());


            }

            @Override
            public void onFailure(Call<ChangeLogoResponse> call, Throwable t) {

            }
        });

答案 1 :(得分:0)

你可以尝试这样你需要将map参数设置为multipart看下面的例子我在这里传递userId和image

RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);

MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
MultipartBody.Part id = MultipartBody.Part.createFormData("userId", userId);
Call<ProfilePicUpdateResponse> call = apiService.updateProfilePic(id,fileToUpload);

答案 2 :(得分:0)

@POST("{path}")
Call<Void> uploadFile(@Header("Content-Type") String type,  @Body RequestBody photo,  @Path("path") String path);


    File file = new File("YOUR_FILE_URI");

    String filename = file.getName();
    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    final String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);


    InputStream in = null;
    RequestBody requestBody = null;
    try {
        in = new FileInputStream(file);
        byte[] buf;
        buf = new byte[in.available()];
        while (in.read(buf) != -1);
        requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), buf);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
    Call<Void> call = getResponse.uploadFile(type, requestBody , posturl);

答案 3 :(得分:0)

使用这个很棒的库:[在后台轻松上传文件(FTP / Multipart / Binary)并附上进度指示通知] https://github.com/gotev/android-upload-service/wiki/Setup它将为您完成剩下的工作。