2015-08-12 16 views
8

Tôi đang đối mặt với một vấn đề để thực hiện một góc cạnh với jax-rs trên backend. Crud rất đơn giản, một số trường văn bản và một trường hình ảnh.Tôi làm cách nào để tải lên hình ảnh và dữ liệu trong cùng một yêu cầu với góc cạnh và an toàn?

Tôi có đoạn code làm việc để tải lên một hình ảnh:

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

Và trong lớp 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> 

Vì vậy, câu hỏi của tôi là làm thế nào tôi có thể làm điều này trong một bước như thế này:

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

Nếu tôi cố gắng gọi mã ở trên từ lớp xem, con ruồi hoang dã đưa ra lỗi không tìm thấy dữ liệu vào thùng d với tham số 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 

Có ai biết cách tôi có thể làm điều đó không? Hay là tôi làm điều đó theo hai bước?

+0

Không có gì thú vị về điều này .. – tariksbl

Trả lời

13

Về mặt kỹ thuật, bạn chỉ có thể nhận cả hai phần dữ liệu từ MultipartFormDataInput. Ví dụ

<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(); 
} 

Nếu bạn muốn đưa mọi thứ vào một POJO, bạn có thể làm một cái gì đó như thế này

public class MyEntity { 

    @FormParam("firstname") 
    private String firstname; 

    @FormParam("lastname") 
    private String lastname; 

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

    // Getter and Setters 
} 

Sau đó, trong phương pháp tài nguyên của bạn

@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(); 
} 

Xem thêm:

+0

Đó là công cụ thứ hai hoàn toàn phù hợp với tôi. Cảm ơn! – fdam

Các vấn đề liên quan