2015-01-09 1 views
5

저지 웹 서비스를 사용하여 이미지를 업로드하려고합니다. 저지 클라이언트를 사용하여 이미지를 업로드하고 있습니다. 아래의 은 입력 스트림을 받아 서버에 이미지를 업로드하는 저지 웹 서비스입니다. 그것은 내가 직접 JSP 다중 형태로 업로드를 사용하여 호출 할 때 잘 작동하지만 난 저지 클라이언트를 사용하여 이미지를 업로드 할 때 실패저지 이미지 업로드 클라이언트

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

이 이미지를 업로드 저지 클라이언트이며, 클라이언트 코드에서 호출하는 다른 웹 서비스의 일부입니다 PHP는 휴식 클라이언트와 이미지를 업로드 저지 웹 서비스에이 저지 클라이언트 호출, 나는 저지 클라이언트를 사용하여 업로드 할 때 일하는 괜찮아요하지만 작동하지 않는 이미지를 업로드하려면 저지 웹 서비스를 직접 호출하는 경우.

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) 

여기에 무엇이 누락되어 있는지 확실히 알려주세요. 감사합니다. .

+0

당신이 "작동하지 않는"무엇을 명확히 할 수 방법? 'uploadedInputStream'에 대해'FileInputStream' 타입을 사용하여 정확한 코드로 테스트했는데 잘 작동합니다. 한 가지 내가 바꿀 것입니다 (그것이 나를 위해 실패하지는 않지만)'MediaType.MULTIPART_FORM_DATA_TYPE'은'fdp'에서'MediaType.APPLICATION_OCTET_STREAM_TYPE'까지 –

+0

uploadedInputStream을 저지 webservice로 전달하고 있지만 동일한 내용을 포함하고 있지 않다는 것을 의미합니다 우리가 저지 클라이언트에서 전달한 청크 및 동일한 컨텐츠 유형. –

+0

양식 데이터는 요청의 주요 내용 유형입니다. 그러나 양식 데이터는 부품과 함께 제공되며 각 부품에는 자체 컨텐츠 유형이 있습니다. 파일은 옥텟 스트림이어야하며 양식 데이터가 아니어야합니다. –

답변

10

이 전체 예제 저지 클라이언트와 웹 서비스 하면 클라이언트 코드를 사용하여 이미지를 업로드하는

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

당신의 웹 서비스

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

전체 응용 프로그램 https://github.com/abdotalaat/upladeimageusingjersy

+0

감사합니다. –

+0

모든 것이 괜찮습니까? 전체 프로젝트는 Netbeans 프로젝트입니다. – abdotalaat

+0

고마워요, 훌륭하게 작동합니다 ... ' –