2016-10-11 5 views
0

Java Jersey 클라이언트 (https://jersey.java.net/documentation/latest/client.html) 코드를 수행하는 방법을 파악하는 데 문제가 있습니다. 여러 파일을 업로드 할 수있는 응용 프로그램이 있습니다. 다음 HTML을 참조하십시오. Java Jersey 클라이언트가 명명 된 컨트롤과 관련된 여러 파일 업로드

<form action="http://localhost:8080/app/rest/files/uploadMultipleFiles" method="post" 
 
               enctype="multipart/form-data"> 
 
<p> 
 
    Select a file to Upload to server: 
 
    <input type="file" name="files" size="60" multiple=“multiple”/> 
 
</p> 
 
<input type="submit" value="Upload File" /> 
 
</form>

참고

는 : 모든 파일이 입력 컨트롤 이름 "파일"과 연관 될 것이다 업로드했습니다.

서버 코드는 다음 작업을 수행합니다

@Path("/uploadMultipleFiles") 
@POST 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response uploadFiles(final FormDataMultiPart multiPart) 
{ 
    //Get contents of control named “files” 
    List<FormDataBodyPart> bodyParts = multiPart.getFields("files"); 
    /* Save multiple files */ 
    if (bodyParts != null) 
    { 

     for (int i = 0; i < bodyParts.size(); i++) 
     { 

      BodyPartEntity bodyPartEntity = (BodyPartEntity) bodyParts.get(i).getEntity(); 
      String fileName = bodyParts.get(i).getContentDisposition().getFileName(); 
      try 
      { 
       String path = temporaryUploadDirectory + File.separator + fileName; 
       long size = saveFile(bodyPartEntity.getInputStream(), path); 

.... HTML 입력 제어 이름 "files"를 사용하여 업로드 된 파일 목록을 얻습니다. 그런 다음 각 파일을 BodyPartEntity로 액세스 할 수 있습니다.

저지 클라이언트가 코드를 업로드하여 파일이 제어 이름 "파일"과 연결되도록하는 방법을 찾는 데 큰 어려움이 있습니다. 이 방법으로 서버 코드를 업로드하기 전에 클라이언트 코드를 작성하고 싶습니다 (서버 코드가 제대로 작동하기 전에 HTML 조각이 생성됩니다 .Jersey 클라이언트 코드가 정확히 같은 방식으로 업로드 할 데이터를 보내고 싶지만 어떻게 할 수 있는지 이해하지 못합니다).

업로드 할 각 파일에 대해 FileDataBodyPart를 만드는 코드를 함께 포스팅 할 수 있습니다. 하지만 컨트롤로 이름이 "파일"을 연결하는 방법을 모른다 :

List<FileDataBodyPart> bodyParts = new ArrayList<FileDataBodyPart>(); 
//get files to upload from file system 
File dir = new File(classLoader.getResource("uploadTestDirectory").getFile()); 
if (dir.exists()) 
{ 
    File[] files = dir.listFiles(); 
    int count = 0; 
    forFile f : files) 
    { 
     //Create a FileDataBodyPart for each file from uploadTestDirectory. Add to a list 
     bodyParts.add(new FileDataBodyPart("file" 
         + count, new File(f.getAbsolutePath()), 
         MediaType.APPLICATION_OCTET_STREAM_TYPE)); 
     count++;   
    } 

    WebTarget webTarget = client.target("http://localhost:8080/app/rest").path("files") 
            .path("uploadMultipleFiles"); 

    //FOLLOWING IS THE FormDataMultiPart to upload 
    FormDataMultiPart multiPart = new FormDataMultiPart(); 
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); 

    //add the file body parts 
    for (FileDataBodyPart bp : bodyParts) 
    { 
     multiPart.bodyPart(bp); 
    } 

    Response clientResponse = 
      webTarget.request(MediaType.APPLICATION_JSON).post(
       Entity.entity(multiPart, multiPart.getMediaType())); 
} 

에게 나의 질문 : 파일합니다 (FileDataBodyPart 개체)라는 이름의 제어에 포함되도록 나는 위의 코드를 수정하는 방법 "파일"(내 서버 코드가 기대하는 것처럼 상단의 HTML이 생성합니다).

위와 같은 클라이언트 코드를 사용하여 파일을 업로드 할 수는 있지만 "파일"컨트롤과 연결하는 방법을 알 수 없습니다. 이게 말이 돼?

도움을 주시면 감사하겠습니다. -Andrew

답변