2015-01-09 20 views
5

Resim, webservice'i kullanarak resim yüklemeye çalışıyorum, resim yüklemek için jarse istemcisini kullanıyorum. , giriş akışını alan ve görüntüyü sunucuya yükleyen jersey web servisidir. o ben doğrudan jsp çok parçalı formu yüklemesidir diyoruz zaman düzgün çalıştığı ama jersey müşteriye kullanarak resim yüklemek zaman başarısızJersey Resim Upload İstemcisi

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

Resmi upload Jersey Client, istemci kod çağrılır başka bir web hizmetinin bir parçasıdır php dinlenme istemcisi ve bu jarse istemcisi, jersey web servisine resmi yüklemek için doğrudan jersey web servisini aramamı isterim, eğer bu işin güzel olduğunu imajı yüklemek için jersey web servisi ama jersey istemcisini kullanarak yüklediğimde çalışmıyor.

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) 

Lütfen burada eksik olduğumdan emin olma konusunda bana yardımcı olun. Teşekkürler.

+0

sen "çalışmıyor" netleştirmek Can anlamına geliyor? Tam kodunuz ile 'uploadedInputStream' için 'FileInputStream' türünü kullanarak test ettim ve iyi çalışıyor. Değiştireceğim bir şey (benim için başarısız olmasına rağmen) MediaType.MULTIPART_FORM_DATA_TYPE' için 'fdp' '' MediaType.APPLICATION_OCTET_STREAM_TYPE'' için –

+0

Yüklediğim webservice için uploadedInputStream geçiyor, ancak aynı içermiyor jersey istemcisinden geçtiğimiz parçalar ve aynı içerik türü. –

+0

form verileri, isteğin ana içerik türüdür. Ancak form verileri parçalarla birlikte gelir ve her bölüm kendi içerik türüne sahiptir. Dosya octet-stream olmalı ve form-data değil –

cevap

10

bu tam bir örnek forması istemcisi ve webservice size müşteri kodu kullanarak resim yüklemek için

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

sizin webservice

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

Tam uygulama https://github.com/abdotalaat/upladeimageusingjersy

+0

Teşekkürler, deneyeyim. –

+0

her şey senin için uygun mu? tüm projenin Netbeans projesinin – abdotalaat

+0

olduğunu unutmayın, harika çalışıyor ... ' –