2017-09-07 84 views
0

接口:與改造上傳圖像2

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

調用API:

File file = new File(model.getImage().getPath()); 
if (!file.exists()) return null; 
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file); 
MultipartBody.Part filePart = MultipartBody.Part.createFormData(ApiConstant.PICTURE_UPLOAD_PARAM, file.getName(), requestBody); 

dataService.uploadPassportImage(map, filePart) 
       .subscribeOn(Schedulers.newThread()) 
       .observeOn(AndroidSchedulers.mainThread()); 

我使用這個方法來上傳圖片到服務器,但服務器無法驗證它作爲一個圖像,因此給我一個迴應,如

「提供的文件不是有效的P icture。請提供PNG/JPG文件」

不過,我已經上傳過郵遞員相同的圖像文件,它是成功的這裏是要求:(注:passportnumber是一個參數,可以不是一個表單數據) enter image description here

回答

0

U可以嘗試這樣的u需要設置地圖參數爲multipart看看下面的例子在這裏,我路過都帳戶及圖像

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); 
+0

在我的情況,不同的是用戶ID不是從數據,它是一個PARAMS。我試過這個解決方案,並且因爲服務器找不到參數值而出現錯誤響應。 – CodeCameo

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); 
1
@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) { 

      } 
     });