2015-01-09 18 views
5

Tôi đang cố gắng tải hình ảnh lên bằng dịch vụ web Jersey, tôi đang sử dụng ứng dụng khách áo để tải hình ảnh lên. dưới đây là dịch vụ web jersey đưa inputstream và tải lên hình ảnh trên máy chủ. nó hoạt động tốt khi tôi trực tiếp gọi nó là sử dụng jsp upload hình thức nhiều phần dữ liệu nhưng không thành công khi tôi tải lên hình ảnh sử dụng jersey clientKhách hàng tải lên hình ảnh Jersey

@POST 
@Path("/upload") 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response uploadFile(
     @FormDataParam("file") InputStream uploadedInputStream, 
     @FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceException 
{ 
    // upload code 
} 

Dưới đây là Jersey Client để upload hình ảnh, mã khách hàng là một phần của một dịch vụ web được gọi là từ php khách hàng phần còn lại và áo khách hàng này gọi dịch vụ web jersey để tải lên hình ảnh, nếu tôi trực tiếp gọi dịch vụ web jersey để tải lên hình ảnh hoạt động tốt nhưng nó không hoạt động khi tôi tải lên bằng cách sử dụng ứng dụng khách.

ClientConfig config = new DefaultClientConfig(); 
Client client = Client.create(config); 
client.setChunkedEncodingSize(1024); 
WebResource wr = client 
     .resource("http://localhost:8080/rest/upload"); 

String contentDisposition = "attachment; filename=\"" 
     + fileDetail.getName() + "\""; 
FormDataMultiPart form = new FormDataMultiPart(); 
ContentDisposition contentDisposition2 = new ContentDisposition(contentDisposition); 
form.setContentDisposition(contentDisposition2); 
FormDataBodyPart fdp = new FormDataBodyPart("file", 
     uploadedInputStream, MediaType.MULTIPART_FORM_DATA_TYPE); 
form.bodyPart(fdp); 
ClientResponse response = wr.type(MediaType.MULTIPART_FORM_DATA).post(
     ClientResponse.class, form) 

Hãy giúp tôi không chắc chắn những gì tôi đang thiếu ở đây. Cảm ơn.

+0

Bạn có thể làm rõ những gì "không làm việc" có nghĩa? Tôi đã thử nghiệm với mã chính xác của bạn, sử dụng loại 'FileInputStream' cho' uploadedInputStream' và nó hoạt động tốt. Một điều tôi sẽ thay đổi (mặc dù nó không làm cho nó thất bại đối với tôi) là 'MediaType.MULTIPART_FORM_DATA_TYPE' cho' fdp' đến 'MediaType.APPLICATION_OCTET_STREAM_TYPE' –

+0

Tôi có nghĩa là nó đang chuyển uploadInputStream sang dịch vụ web jersey nhưng nó không chứa cùng khối và cùng loại nội dung mà chúng tôi đã chuyển từ ứng dụng khách trên áo. –

+0

biểu mẫu dữ liệu là loại nội dung chính của yêu cầu. Nhưng dữ liệu biểu mẫu đi kèm với các bộ phận và mỗi phần có loại nội dung riêng. Tệp phải là octet-stream và không phải là dữ liệu mẫu –

Trả lời

10

ví dụ đầy đủ này để upload hình ảnh sử dụng client jersey và webservice bạn mã khách hàng

public class Test { 

    private static URI getBaseURI() { 
     return UriBuilder.fromUri("http://localhost:8080/restfullwebservice/resources/generic").build(""); 
    } 

    public static void main(String[] args) throws FileNotFoundException { 
     final ClientConfig config = new DefaultClientConfig(); 
     final Client client = Client.create(config); 

     final WebResource resource = client.resource(getBaseURI()).path("upload"); 

     final File fileToUpload = new File("C:/Users/Public/Pictures/Desert.jpg"); 

     final FormDataMultiPart multiPart = new FormDataMultiPart(); 
     if (fileToUpload != null) { 
      multiPart.bodyPart(new FileDataBodyPart("file", fileToUpload, 
        MediaType.APPLICATION_OCTET_STREAM_TYPE)); 
     } 

     final ClientResponse clientResp = resource.type(
       MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, 
       multiPart); 
     System.out.println("Response: " + clientResp.getClientResponseStatus()); 

     client.destroy(); 
    } 
} 

webservice của bạn

@POST 
@Path("upload") 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public void uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceChannelException { 
    OutputStream os = null; 
    try { 
     File fileToUpload = new File("C:/Users/Public/Pictures/Desert1.jpg"); 
     os = new FileOutputStream(fileToUpload); 
     byte[] b = new byte[2048]; 
     int length; 
     while ((length = uploadedInputStream.read(b)) != -1) { 
      os.write(b, 0, length); 
     } 
    } catch (IOException ex) { 
     Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex); 
    } finally { 
     try { 
      os.close(); 
     } catch (IOException ex) { 
      Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

Full ứng dụng https://github.com/abdotalaat/upladeimageusingjersy

+0

Cảm ơn, hãy để tôi thử. –

+0

mọi thứ đều ổn với bạn? lưu ý rằng dự án đầy đủ là dự án Netbeans – abdotalaat

+0

Cảm ơn, nó hoạt động tuyệt vời ... ' –

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