2016-04-23 35 views
6

Devo inviare una richiesta di posta in questo formato.Impossibile inviare dati multipart da retrofit 2

--__X_PAW_BOUNDARY__ 
Content-Disposition: form-data; name="user_photo[image]"; filename="file.jpg" 
Content-Type: image/jpeg 

ÿØÿàJFIFHHÿáLExifMM*i     
ÿí8Photoshop 3.08BIM8BIM%ÔÙ²é ìøB~ÿÀ 
 "ÿÄ  
ÿĵ}!1AQa"q2¡#B±ÁRÑð$3br  
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ 
ÿĵw!1AQaq"2B¡±Á #3RðbrÑ 
$4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÛC          ÿÛC                                                   ÿÝZÿÚ?ü_¢+þæð¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¯ë3þ 

sto fondamentalmente cercando di inviare una foto a richiesta POST con il nome user_photo[image] come quello sull'immagine illustrato di seguito:

enter image description here

Ecco frammenti delle soluzioni che ho considerati:

Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part MultipartBody.Part file); 


     MultipartBody.Part body = MultipartBody.Part.createFormData("user_photo[image]", file.getName(), requestFile); 
Call<models.UploadResponse> call = userRequest.uploadPhoto(62, body); 



    RequestBody requestFile = 
      RequestBody.create(MediaType.parse("image/*"), FileUtils.getFile(PhotoUploadActivity.this, fileUri)); 
    Call<models.UploadResponse> call = userRequest.uploadPhoto(62, requestFile); 

    Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part ("name=\"user_photo[image]\"") RequestBody file); 

    @Multipart 
     @POST("users/{id}/user_photos") 
    Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part("name=\"user_photo[image]\"") RequestBody file); 

Questo è il log okhttp:

D/OkHttp: Content-Type: multipart/form-data; boundary=75e8ae2e-a160-413f-82d1-3afd94f22c43 
D/OkHttp: Content-Length: 43706 
D/OkHttp: ������JFIF�����������������Photoshop 3.0��8BIM����������hg��vdP4uk_QnzZ02cW1f845(��JFBMD0f00078d0300006c1d0000675000009d53000068590000c77e000041a90000b1a90000��ICC_PROFILE������lcms����mntrRGB XYZ ���������)��9acspAPPL�����������������������������������������������������������������-lcms���������������������������������������������������������������������������������������������� 
D/OkHttp: desc�������������^cprt����\������wtpt����h������bkpt����|������rXYZ�����������gXYZ�����������bXYZ�����������rTRC�����������@gTRC�����������@bTRC�����������@desc��������������c2��������������������������������������������������������������������������������������������������������������������������������������������������������������������text��������FB����XYZ �������������������������-XYZ ����������������3�����XYZ ������������o�����8������XYZ ������������b������������XYZ ������������$������������curv����������������������c�k�?Q4!�)�2;�FQw]�kpz���|�i�}���0������C�� 
D/OkHttp: 
D/OkHttp: 
D/OkHttp: �"�������������������������������������������������������������������������<r9������J�������@��[email protected]�|2�=�VW�}������E���� �,i�v!�Q;j�H�[email protected]!��1�J�T5L4�������� ��h���U��S�׿м�6v���2��] 
D/OkHttp: ����3��T0��[email protected]�`&h��������%̬�Η�����[w(��جƥ�f^ 
D/OkHttp: C���;���t������E�͂I�O�[�Hvݝ -�g߫t�k���y����ة�ۼ�6S7zEe��/��I_��b��;�Gr��/�:B��R-7$eO��M��G�۱Iū�G2�r�s7#p�P��Ha�o6U�$�3(�����G\�q�4�ȋ�Dj�S�������n�V���J�f?m{{��ޣq�<\��_L���v+�1��-�\h���\u�Pnms����9 ��".~N�z�*J>f�I�:$��T j�C�{���$�_$3y���ߪy�¼žj���j�愰�s�z�)�o���J����]�˭���ŧ��s]��� 
D/OkHttp: ��)��(�u�����şLl_T���S��"(��_o�{n�3���}OU�P`}C^zl�]޹V"}��u�Un|=���ο-�mn�e�1�g���S���Q0ދggF������B�;ӫ>��ӷ�P���5c�x"�#o�9螗�s`�j2����O�Y�ӆa��-�便�zy���d��� 
D/OkHttp: E�r�jqL�ah�^�u��'�+�Z����N:{a�'N��1���H�M�t �H���i���û�Ԕ�hP 
D/OkHttp: ���0��� 
D/OkHttp: �����Y#M�qg���+��?��T�����ܱ���4�@��[email protected]����������&����G����wL���<Vo�l�Q�۲����H_?a�I�ũT��z��H���Z����S�b)���d���L11�3������hL �����h����b� ���� 
D/OkHttp: <Rf"��L��(� 
D/OkHttp: s��3�Pgı��`g��ω��>&|J�S%���]�b�m�q#Ө�k�� 

anche:

Solutions ho cui:

  1. https://github.com/square/retrofit/issues/1140
  2. https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server
  3. Retrofit - Multipart request: Required MultipartFile parameter 'file' is not present
  4. Multipart Retrofit 2.0 image upload
  5. https://github.com/square/retrofit/issues/1063

E sono stanco di cercare soluzioni. Qualcuno può aiutare?

+0

URL del servizio bisogno, di token, ecc ... da testare. Ho risolto i miei problemi guardando il quinto link sulla tua domanda –

risposta

1

Devi inviarlo come file digitato.

TypedFile upload_file = new TypedFile("multipart/form-data", new File(your_file_location)); 

@Multipart 
@POST("users/{id}/user_photos") 
Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part("name=\"user_photo[image]\"")TypedFile upload_file); 

Speranza che aiuta

+0

Non c'è TypedFile in retrofit 2 –

7

Al fianco API

@Multipart 
    @POST("users/{id}/user_photos") 
    Call<SignUpResp> uploadPhoto(@Part("description") RequestBody description, @Part MultipartBody.Part file,(add if more parameter req as like ID,name)); 

al Java lato di file

if (fileUpload != null) { 


     /** 
     * code for multipart 
     */ 

     // create RequestBody instance from file 
     RequestBody requestFile = 
       RequestBody.create(MediaType.parse("multipart/form-data"), fileUpload); 

     // MultipartBody.Part is used to send also the actual file name 
     MultipartBody.Part body = 
       MultipartBody.Part.createFormData("user_photo[image]", fileUpload.getName(), requestFile); 

     // add another part within the multipart request 
     String descriptionString = "hello, this is description speaking"; 
     RequestBody description = 
       RequestBody.create(
         MediaType.parse("multipart/form-data"), descriptionString); 


     call = userRequest.uploadPhoto(description, body, authData); 

    } 
+0

Grazie per la tua risposta ma non la richiesta è come questa http://i.imgur.com/r13cBDE.png –

+0

è il problema del webservice ?? –

+0

Nessun servizio Web funziona correttamente quando viene chiamato dalla console di resto. –

1

cambiamento

@Part("name=\"user_photo[image]\"") RequestBody file)

a

@Part("user_photo[image]\"; filename=\"file.jpg\" ") 

Creare requestBody del file utilizzando

RequestBody fileBody = RequestBody.create(MediaType.parse("image"), myImageFile); 

È possibile trovare maggiori informazioni su questo su

https://github.com/square/retrofit/issues/1140

In questo caso il nome del file file.jpg è hardcoded si può anche avere nomi di file dinamici.Anche se non ho provato la dinamica dei file di denominazione, ma sotto è il link che potrebbe funzionare

https://github.com/square/retrofit/issues/1063#issuecomment-145920568

2

ho usato this lib e funziona come un fascino per il caricamento multiparts

+0

Upvote per la tua condivisione, grazie – BNK

1

ho usato i seguenti metodi, non sono in retrofit, la speranza che aiuta: laterale Server: applicazione Primavera di avvio java:

@RequestMapping(value="/applyeffect", method=RequestMethod.POST,produces=MediaType.APPLICATION_OCTET_STREAM_VALUE) 
    public @ResponseBody byte[] applyEffect(@RequestParam("userid") String userId, @RequestParam("type") int effectType,@RequestParam("file") MultipartFile file){ 

     try { 
       byte[] bytes = file.getBytes(); 
       File temp=new File(file.getName()); 
       BufferedOutputStream stream = 
         new BufferedOutputStream(new FileOutputStream(temp)); 
       stream.write(bytes); 
       stream.close(); 
       File newFile=null; 
       if(effectType==1){ 
        newFile=ImageUtils.applyTint(userId, temp, 50); 
       }else if(effectType==2){ 
        newFile=ImageUtils.applyEffect(userId, temp, 128); 
       }else if(effectType==3){ 
        newFile=ImageUtils.applyBlackWhiteEffect(userId,temp, 128); 
       } 






       FileInputStream input=new FileInputStream(newFile); 
       byte[]data=IOUtils.toByteArray(input); 


       return data; 

      } catch (Exception e) { 
       System.out.println(e.toString()); 
       return null; 
      } 
    } 

Lato client Android: Ho usato questa classe util https://github.com/MinaSamy/DailySelfie/blob/master/app/src/main/java/bloodstone/dailyselfie/android/utils/PostMultiPart.java

Problemi correlati