--- title: "Document OCR" slug: "ai-application-service-ocr-example02" updated: 2026-07-23T09:04:22Z published: 2026-07-23T09:04:22Z canonical: "api.ncloud-docs.com/ai-application-service-ocr-example02" --- > ## Documentation Index > Fetch the complete documentation index at: https://api.ncloud-docs.com/llms.txt > Use this file to discover all available pages before exploring further. # Document OCR Classic/VPC 환경에서 이용 가능합니다. CLOVA OCR 서비스의 Document OCR API 예제를 소개합니다. ## `Content-Type: multipart/form-data`인 경우 요청 헤더 `Content-Type`이 `multipart/form-data`인 경우의 API 예제를 설명합니다. ### Java Java 기반의 API 예제 코드는 다음과 같습니다. ``` import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.UUID; import org.json.JSONArray; import org.json.JSONObject; public class OCRAPIDemo { public static void main(String[] args) { String apiURL = "YOUR_API_URL"; String secretKey = "YOUR_SECRET_KEY"; String imageFile = "YOUR_IMAGE_FILE"; try { URL url = new URL(apiURL); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.setReadTimeout(30000); con.setRequestMethod("POST"); String boundary = "----" + UUID.randomUUID().toString().replaceAll("-", ""); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); con.setRequestProperty("X-OCR-SECRET", secretKey); JSONObject json = new JSONObject(); json.put("version", "V2"); json.put("requestId", UUID.randomUUID().toString()); json.put("timestamp", System.currentTimeMillis()); JSONObject image = new JSONObject(); image.put("format", "jpg"); image.put("name", "demo"); JSONArray images = new JSONArray(); images.put(image); json.put("images", images); String postParams = json.toString(); con.connect(); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); long start = System.currentTimeMillis(); File file = new File(imageFile); writeMultiPart(wr, postParams, file, boundary); wr.close(); int responseCode = con.getResponseCode(); BufferedReader br; if (responseCode == 200) { br = new BufferedReader(new InputStreamReader(con.getInputStream())); } else { br = new BufferedReader(new InputStreamReader(con.getErrorStream())); } String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = br.readLine()) != null) { response.append(inputLine); } br.close(); System.out.println(response); } catch (Exception e) { System.out.println(e); } } private static void writeMultiPart(OutputStream out, String jsonMessage, File file, String boundary) throws IOException { StringBuilder sb = new StringBuilder(); sb.append("--").append(boundary).append("\r\n"); sb.append("Content-Disposition:form-data; name=\"message\"\r\n\r\n"); sb.append(jsonMessage); sb.append("\r\n"); out.write(sb.toString().getBytes("UTF-8")); out.flush(); if (file != null && file.isFile()) { out.write(("--" + boundary + "\r\n").getBytes("UTF-8")); StringBuilder fileString = new StringBuilder(); fileString .append("Content-Disposition:form-data; name=\"file\"; filename="); fileString.append("\"" + file.getName() + "\"\r\n"); fileString.append("Content-Type: application/octet-stream\r\n\r\n"); out.write(fileString.toString().getBytes("UTF-8")); out.flush(); try (FileInputStream fis = new FileInputStream(file)) { byte[] buffer = new byte[8192]; int count; while ((count = fis.read(buffer)) != -1) { out.write(buffer, 0, count); } out.write("\r\n".getBytes()); } out.write(("--" + boundary + "--\r\n").getBytes("UTF-8")); } out.flush(); } } ``` ### Python Python 기반의 API 예제 코드는 다음과 같습니다. ``` import requests import uuid import time import json api_url = 'YOUR_API_URL' secret_key = 'YOUR_SECRET_KEY' image_file = 'YOUR_IMAGE_FILE' request_json = { 'images': [ { 'format': 'jpg', 'name': 'demo' } ], 'requestId': str(uuid.uuid4()), 'version': 'V2', 'timestamp': int(round(time.time() * 1000)) } payload = {'message': json.dumps(request_json).encode('UTF-8')} files = [ ('file', open(image_file,'rb')) ] headers = { 'X-OCR-SECRET': secret_key } response = requests.request("POST", api_url, headers=headers, data = payload, files = files) print(response.text.encode('utf8')) ``` ### PHP PHP 기반의 API 예제 코드는 다음과 같습니다. ``` version = "V2"; $params->requestId = uniqid(); $params->timestamp = time(); $image->format = "jpg"; $image->name = "demo"; $images = array($image); $params->images = $images; $json = json_encode($params); $boundary = uniqid(); $is_post = true; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, $is_post); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $post_form = array("message" => $json, "file" => new CURLFILE($image_file)); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_form); $headers = array(); $headers[] = "X-OCR-SECRET: ".$client_secret; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $err = curl_error($ch); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close ($ch); echo $status_code; if($status_code == 200) { echo $response; } else { echo "ERROR: ".$response; } ?> ``` ## `Content-Type: application/json`인 경우 요청 헤더 `Content-Type`이 `application/json`인 경우의 API 예제를 소개합니다. ### Java Java 기반의 API 예제 코드는 다음과 같습니다. ``` import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.UUID; import org.json.JSONArray; import org.json.JSONObject; public class OCRGeneralAPIDemo { public static void main(String[] args) { String apiURL = "YOUR_API_URL"; String secretKey = "YOUR_SECRET_KEY"; try { URL url = new URL(apiURL); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json; charset=utf-8"); con.setRequestProperty("X-OCR-SECRET", secretKey); JSONObject json = new JSONObject(); json.put("version", "V2"); json.put("requestId", UUID.randomUUID().toString()); json.put("timestamp", System.currentTimeMillis()); JSONObject image = new JSONObject(); image.put("format", "jpg"); image should be public, otherwise, should use data FileInputStream inputStream = new FileInputStream("YOUR_IMAGE_FILE"); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); inputStream.close(); image.put("data", buffer); image.put("name", "demo"); JSONArray images = new JSONArray(); images.put(image); json.put("images", images); String postParams = json.toString(); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(postParams); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); BufferedReader br; if (responseCode == 200) { br = new BufferedReader(new InputStreamReader(con.getInputStream())); } else { br = new BufferedReader(new InputStreamReader(con.getErrorStream())); } String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = br.readLine()) != null) { response.append(inputLine); } br.close(); System.out.println(response); } catch (Exception e) { System.out.println(e); } } } ``` ### Python Python 기반의 API 예제 코드는 다음과 같습니다. ``` import requests import uuid import time import base64 import json api_url = 'YOUR_API_URL' secret_key = 'YOUR_SECRET_KEY' image_file = 'YOUR_IMAGE_FILE' with open(image_file,'rb') as f: file_data = f.read() request_json = { 'images': [ { 'format': 'jpg', 'name': 'demo', 'data': base64.b64encode(file_data).decode() } ], 'requestId': str(uuid.uuid4()), 'version': 'V2', 'timestamp': int(round(time.time() * 1000)) } payload = json.dumps(request_json).encode('UTF-8') headers = { 'X-OCR-SECRET': secret_key, 'Content-Type': 'application/json' } response = requests.request("POST", api_url, headers=headers, data = payload) print(response.text) ``` ### PHP PHP 기반의 API 예제 코드는 다음과 같습니다. ``` version = "V2"; $params->requestId = "uuid"; $params->timestamp = time(); $image->format = "jpg"; $image->data = base64_encode(file_get_contents($image_file)); $image->name = "demo"; $images = array($image); $params->images = $images; $json = json_encode($params); $is_post = true; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, $is_post); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $headers = array(); $headers[] = "X-OCR-SECRET: ".$client_secret; $headers[] = "Content-Type:application/json; charset=utf-8"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $err = curl_error($ch); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close ($ch); echo $status_code; if($status_code == 200) { echo $response; } else { echo "ERROR: ".$response; } ?> ```