2016-07-14 2 views
0

파일과 일반 텍스트 데이터가 포함 된 양식을 게시하려고합니다. Apache CXF를 사용하고 있습니다. 아래 코드는apache cxf를 사용하여 서식 게시

WebClient client= WebClient.create(ROOT_URL_FILE_SERVICE); 

     client.type("multipart/form-data");  
     InputStream is= new ByteArrayInputStream(getBytes());  
     List<Attachment> attachments= new ArrayList(); 
     ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg"); 
     Attachment att= new Attachment("File", is, cd); 
     Attachment pageNumber= new Attachment("DATA1", MediaType.TEXT_PLAIN, "1"); 
     Attachment OutputType= new Attachment("DATA2", MediaType.TEXT_PLAIN, "2"); 

     attachments.add(att); 
     attachments.add(pageNumber); 
     attachments.add(OutputType); 
     MultipartBody body= new MultipartBody(attachments); 

     Response res=client.post(body); 

서버 측에서 (파일, DATA1, DATA2)을 가져올 수 없습니다.

위 코드에서 수정해야하는 것은 무엇입니까.

답변

2

서버 측 cxf 구성을 확인하는 방법은 다음과 같습니다.

@POST 
@Path("/upload") 
@Produces(MediaType.TEXT_XML) 
public String upload(
     @Multipart(value = "File", type = MediaType.APPLICATION_OCTET_STREAM) final InputStream fileStream, 
     @Multipart(value = "DATA1", type = MediaType.TEXT_PLAIN) final String fileNumber, 
     @Multipart(value = "DATA2", type = MediaType.TEXT_PLAIN) final String outputType) { 
    BufferedImage image; 
    try { 
     image = ImageIO.read(fileStream); 
     LOG.info("Received Image with dimensions {}x{} ", image.getWidth(), image.getHeight()); 
    } catch (IOException e) { 
     LOG.error(e.getMessage(), e); 
    } 

    LOG.info("Received Multipart data1 {} ", fileNumber); 
    LOG.info("Received Multipart data2 {} ", outputType); 
    return "Recieved all data"; 
} 

테스트 된 클라이언트 파일

public static void main(String[] args) throws IOException { 

      WebClient client = WebClient.create("http://localhost:8080/services/kp/upload"); 
      ClientConfiguration config = WebClient.getConfig(client); 
      config.getInInterceptors().add(new LoggingInInterceptor()); 
      config.getOutInterceptors().add(new LoggingOutInterceptor()); 
      client.type("multipart/form-data"); 
      InputStream is = FileUtils.openInputStream(new File("vCenter_del.jpg")); 
      List<Attachment> attachments = new ArrayList<>(); 
      ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg"); 
      Attachment att = new Attachment("File", is, cd); 
      Attachment pageNumber = new Attachment("DATA1", MediaType.TEXT_PLAIN, "1"); 
      Attachment OutputType = new Attachment("DATA2", MediaType.TEXT_PLAIN, "2"); 

      attachments.add(att); 
      attachments.add(pageNumber); 
      attachments.add(OutputType); 
      MultipartBody body = new MultipartBody(attachments); 

      Response res = client.post(body); 

      String data = res.readEntity(String.class); 
      System.out.println(data); 
     } 

참고 : 컨텐츠 ID에 일치하지 않는 경우 짧은에서 해당 파일, 데이터 1 또는 콘텐츠 형식은 서버 모두에서 데이터를받을하지 않을 수 있습니다 경우 당신은 400과 415와 같은 적절한 오류를 얻을 것이다.

관련 문제