2016-04-12 9 views
0

Ich versuche, ein Fileinputstream mit Retrofit 1.9.x POST (ich kann noch nicht 2.0.x bewegen)POSTen einen Fileinputstream mit Retrofit 1.9.x

Ich habe This Post lesen.

In diesem Beitrag habe ich verstanden, dass, wenn ich eine TypedInput in meiner Schnittstelle und implementieren TypedInput-Klasse Wrapper, die den Stream behandelt, dann sollte es funktionieren. Es war unklar, ob TypedInput vs TypedOutput die Antwort war (TypedInput klang am korrektesten, die verknüpfte Retrofit-Dokumentation nicht so weit wie ich sagen konnte. Auch ist es auf 2.0 verschoben)

Um fortzufahren - Ich habe eine erstellt

Klasse
private class InputStreamMunger implements TypedInput { 
    private InputStream is; 
    private String mimeType; 
    private Long fileLength; 

    public InputStreamMunger(InputStream is, String mimeType, Long fileLength) { 
     this.is = is; 
     this.fileLength = fileLength; 
     this.mimeType = mimeType; 
    } 

    @Override 
    public String mimeType() { 
     return mimeType; 
    } 

    @Override 
    public long length() { 
     return fileLength; 
    } 

    @Override 
    public InputStream in() throws IOException { 
     return is; 
    } 
} 

Meine Schnittstelle:

@Multipart 
@POST("/MrService/v1/upload/{accountId}") 
Response upload(
    @Path("accountId") String accountId, 
    @Part("file") TypedInput file); 

Dann rufe ich es

FileInputStream is = new FileInputStream("src/test/java/com/me/MrService/tester.txt"); 
    InputStreamMunger file ; 

    try { 
     file = new InputStreamMunger(is, "text/plain", is.getChannel().size()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return; 
    } 

    Response r = client.upload("12345", file); 

Der Fehler, den ich bekommen ist:

retrofit.RetrofitError: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.me.MrService.IntegrationTestIT$InputStreamMunger and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)) 

Bedeutet dies, dass ich meine eigenen Mapper erstellen Strom zu handhaben? Ich hoffe, dass ich nur etwas falsch mache und dass ich nicht durch diesen Reifen springen muss.

Danke!

Antwort

0

Am Ende habe ich den TypedOutput anstelle von TypedInput implementiert.

private class InputStreamMunger implements TypedOutput { 

    private InputStream is; 
    private String mimeType; 
    private Long fileLength; 
    private String fileName; 
    private static final int BUFFER_SIZE = 4096; 


    public InputStreamMunger(InputStream is, String mimeType, Long fileLength, 
      String fileName) { 
     this.is = is; 
     this.fileLength = fileLength; 
     this.mimeType = mimeType; 
     this.fileName = fileName; 
    } 

    @Override 
    public String mimeType() { 
     return mimeType; 
    } 

    @Override 
    public long length() { 
     return fileLength; 
    } 

    @Override 
    public void writeTo(OutputStream out) throws IOException { 
     byte[] buffer = new byte[BUFFER_SIZE]; 
     try { 
      int read; 
      while ((read = is.read(buffer)) != -1) { 
       out.write(buffer, 0, read); 
      } 
     } finally { 
      is.close(); 
     } 
    } 

    public String fileName() { 
     return fileName; 
    } 

} 
Verwandte Themen