2015-08-12 9 views
8

Mam do czynienia z problemem, aby zrobić kątowe crud z jax-rs na zapleczu. Crud jest bardzo prosty, niektóre pola tekstowe i pole obrazu.Jak mogę przesłać obraz i dane w tym samym żądaniu z kanciastą i relaksującą?

mam kod działa załadować obraz:

@POST 
@Consumes("multipart/form-data") 
public Response uploadFile(MultipartFormDataInput input) { 
    ... 
} 

iw warstwie html:

<form action="http://localhost:8080/app/api/user" method="post" enctype="multipart/form-data"> 
    <p> 
    Choose a file : <input type="file" name="file" /> 
    </p> 
    <input type="submit" value="Upload" /> 
</form> 

Więc moje pytanie brzmi: w jaki sposób można zrobić to w jednym kroku jak to:

@POST 
@Consumes("multipart/form-data") 
public Response save(MultipartFormDataInput input, MyEntity entity) { 
    ... 
} 

Jeśli spróbuję wywołać powyższy kod z warstwy widoku, wildfly daje błąd, który nie odbiera danych do pojemnika d z parametrem MyEntity.

[org.jboss.resteasy.core.ExceptionHandler] (default task-3) failed to execute: javax.ws.rs.NotSupportedException: 
Could not find message body reader for type: class mypackage.MyEntity of content type: multipart/form-data;boundary=----WebKitFormBoundaryRXVvqLpZACPylNgS 

Czy ktoś wie, jak mogę to zrobić? Lub shoud robię to w dwóch etapach?

+0

Nic angularjs o tym .. – tariksbl

Odpowiedz

13

Technicznie, możesz po prostu uzyskać obie części danych z MultipartFormDataInput. Na przykład

<form action="api/upload" method="post" enctype="multipart/form-data"> 
    Choose a file : <input type="file" name="file" /> 
    First name: <input type="text" name="firstname" /> 
    List name: <input type="text" name="lastname" /> 
    <input type="submit" value="Upload" /> 
</form> 

@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response upload(MultipartFormDataInput multipart) throws IOException { 

    try (InputStream in = multipart.getFormDataPart("file", InputStream.class, null); 
     FileOutputStream fos = new FileOutputStream("file.png")) { 
     byte[] buff = new byte[1024]; 
     int count; 
     while ((count = in.read(buff)) != -1) { 
      fos.write(buff, 0, count); 
     } 
    } 

    String firstname = multipart.getFormDataPart("firstname", String.class, null); 
    String lastname = multipart.getFormDataPart("lastname", String.class, null); 
    return Response.ok(firstname + ":" + lastname).build(); 
} 

Jeśli chcesz umieścić wszystko w POJO, można zrobić coś takiego

public class MyEntity { 

    @FormParam("firstname") 
    private String firstname; 

    @FormParam("lastname") 
    private String lastname; 

    @FormParam("file") 
    private byte[] file; 

    // Getter and Setters 
} 

Następnie w metodzie zasobów

@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response upload(@MultipartForm MyEntity entity) throws IOException { 

    try (FileOutputStream fos = new FileOutputStream("file.png")) { 
     byte[] filebytes = entity.getFile(); 
     fos.write(filebytes); 
    } 

    String firstname = entity.getFirstname(); 
    String lastname = entity.getLastname(); 
    return Response.ok(firstname + ":" + lastname).build(); 
} 

Zobacz więcej:

+0

To wszystko, drugi działa idealnie dla mnie. Dzięki! – fdam

Powiązane problemy