2017-11-23 4 views
0

첫째, 이미지 및 기타 POJO 클래스의 바이트 배열 필드를 포함하는 POJO 클래스를 보내기위한 휴식 서비스를 생성해야합니다. 또한 저지 클라이언트를 사용하여이 서비스를 사용할 필요가 있습니다. application/octet-stream MediaType을 사용하여 이러한 서비스를 구현할 수 있습니다. 나는 이미 이미지 파일만을 위해 작업을하고있다. 이 작업을 수행하는 올바른 방법은 무엇입니까?application/octet_stream을 통해 저지를 사용하여 휴식 서비스를 만들고 소비하는 방법?

public class Client { 

    private static final String BASE_URI = "http://localhost:8090/Test/gets"; 

    public Client() throws IOException { 

     try { 
      Client client = Client.create(); 
      WebResource objWebResource = client.resource(BASE_URI); 
      ClientResponse response = objWebResource.path("/").queryParam("id", "1") 
          .type(javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM).get(ClientResponse.class); 

      System.out.println("response : " + response); 
      if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) { 

      ResponseSample responseSample = response.getEntity(ResponseSample.class); 

//   InputStream input = (InputStream)response.getEntity(InputStream.class); 
//   BufferedImage bf = ImageIO.read(input); 
//   File outputfile = new File("../test.jpeg"); 
//   ImageIO.write(bf, "jpg", outputfile);   

      ObjectMapper mapper = new ObjectMapper(new BsonFactory()); 
      // deserialize data 
    } 
     } catch (UniformInterfaceException e) { 
      e.printStackTrace(); 
     } catch (ClientHandlerException e) { 
      e.printStackTrace(); 
     } 


    } 

    public static void main(String... args) throws IOException { 
     new Client(); 
    } 

답변

0

나는 마침내 해결책을 발견 :

public class GetDataImage { 

     @GET 
     @Path("/gets") 
     @Produces(MediaType.APPLICATION_OCTET_STREAM) 
     public Response getFile(@QueryParam("id") String id) throws IOException { 

      File file = new 
      File("..\test_image.jpg"); 
      RenderedImage image2 = ImageIO.read(file); 

      Foo foo = new Foo(); 
      Sample sample = new Sample (1, new Byte[] {},foo ); 

      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      ObjectMapper mapper = new ObjectMapper(new BsonFactory()); 
      mapper.writeValue(baos, responseChipoutImage); 

       StreamingOutput stream = new StreamingOutput() { 
        @Override 
        public void write(OutputStream output) throws IOException { 
        try { 
        // ImageIO.write(image2, "jpg", output); 
         new ObjectOutputStream(output).writeObject(responseChipoutImage); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        } 
       }; 

        return Response.ok(stream, "application/octet-stream") 
          .header("content-disposition", "attachment; filename = " + image2.toString()) 
          .build(); 
        } 
} 


public class Sample{ 
     int    sampleId; 
     Byte[]   image; 
     Foo    foo; 

     //constructor 
     //getter setter 
    } 
는 클라이언트입니다. 이 솔루션은 Jackson JSON 파서에 숨겨져 있습니다 -> Bson4jackson. 이런

변경된 서버 측 StreamingOutput ovveride있어서

StreamingOutput stream = new StreamingOutput() { 
       @Override 
       public void write(OutputStream output) throws IOException { 
       try {  
       ObjectMapper mapper = new ObjectMapper(new BsonFactory()); 
       mapper.writeValue(output, responseChipoutImage); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       } 
      }; 

후가되는 InputStream에서 잭슨 bson 파서 추가 클라이언트로부터 데이터를 혔.

public class Client { 
private static final String BASE_URI = "http://localhost:8090/Test/gets"; 

public Client() throws IOException { 

    try { 
     Client client = Client.create(); 
     WebResource objWebResource = client.resource(BASE_URI); 
     ClientResponse response = objWebResource.path("/").queryParam("id", "1") 
      .type(javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM).get(ClientResponse.class); 
     if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) { 

      ObjectMapper mapper = new ObjectMapper(new BsonFactory()).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
      ResponseSample responseSample = mapper.readValue(response.getEntityInputStream(), ResponseSample.class); 
     } 
    } catch (UniformInterfaceException e) { 
     e.printStackTrace(); 
    } catch (ClientHandlerException e) { 
     e.printStackTrace(); 
    } 

} 
public static void main(String...args) throws IOException { 
    new Client(); 
} 
관련 문제