2017-11-27 4 views
0

MS 컴퓨터 비전 APi에 대한 OCR 게시 요청을 보내고 응답을 반환합니다. HTTP/1.1 415 지원되지 않는 미디어 유형 [Cache-Control : no-cache, Pragma : no-cache, Content-Length : 183, Content-Type : application/json;http 응답을 반환하는 컴퓨터 비전 ocr API : Java http 요청에 대해 지원되지 않는 미디어 유형

여기에 내 Java 코드가있어 요청에 multipartfile (jpg)을 추가 한 다음 게시합니다.

HttpClient httpClient = HttpClientBuilder.create().build(); 

    String fileContentType = file.getContentType(); 
    URI uri = buildUri(); 

    if(uri == null){ 
     //throw some exception 
    } 

    HttpPost request = new HttpPost(uri); 

    request.setHeader("Content-Type", fileContentType); 
    request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey); 

    MultipartEntityBuilder mpEB = MultipartEntityBuilder.create(); 


    InputStream fileInputStream = file.getInputStream(); 

    //modify method to add filename if later need arises 
    //link: https://memorynotfound.com/apache-httpclient-multipart-upload-request/ 
    mpEB.addBinaryBody("image", fileInputStream); 
    mpEB.setContentType(ContentType.MULTIPART_FORM_DATA); 

    HttpEntity image = mpEB.build(); 
    request.setEntity(image); 

    HttpResponse response = httpClient.execute(request); 
    HttpEntity entity = response.getEntity(); 

    System.out.println(json.toString()); 
    //dataFromJsonExtractor.extractData(json); 

} 
private URI buildUri(){ 

    URI link = null; 
    try { 
     URIBuilder builder = new URIBuilder(uriBase); 

     builder.setParameter("language", "de"); 

     URI uri = builder.build(); 

     link = uri; 
     }catch (URISyntaxException e){ 
     logger.debug("Computer Vision API URL Builder creation error: " + e); 

    } 

    return link; 
} 

도움이 필요하십니까? 건배!

답변

0

한 번에 하나의 이미지 만 게시 할 수 있으므로 멀티 파트 MIME 본문을 구성하는 대신 POST 본문에 단일 이미지 페이로드를 게시하는 것이 훨씬 간단합니다. 코드는 다음과 같이 수 : API가 콘텐츠 형식이 아닌 image/*application/octet-stream을 기대

public static void run(FileDataSource file) throws Exception 
{ 
    HttpClient httpClient = HttpClientBuilder.create().build(); 

    URI uri = buildUri(); 

    HttpPost request = new HttpPost(uri); 

    request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey); 

    HttpEntity entity = EntityBuilder 
     .create() 
     .setStream(file.getInputStream()) 
     .setContentType(ContentType.APPLICATION_OCTET_STREAM) 
     .build(); 

    request.setEntity(entity); 

    HttpResponse response = httpClient.execute(request); 
    HttpEntity jsonEntity = response.getEntity(); 

    System.out.println(EntityUtils.toString(jsonEntity)); 
} 

하는 것으로.

+0

건배 !! Octet Stream의 ContentType에 대한 매력처럼 작동했습니다. 문서에 있지만 멀티 파트 양식에서는 작동하지 않았습니다. 기묘한. 감사. –