OCRController.java

//@Controller
//public class OCRController {
//    @Autowired
//    private OCRService ocrService;
//
//    @RequestMapping  ("/clovaOCR")
//    public String clovaOCR() {
//        ocrService.clovaOCRService();
//        return "result";
//    }
//
//}

//프론트에서 넘어오면 작성할 코드
//@RestController
//public class OCRController {
//    @Autowired
//    private OCRService ocrService;
//    @RequestMapping("/clovaOCR")
//    // value = clovaOCR
//    public String clovaOCR(@RequestPart("uploadFile") MultipartFile file){
//        String result = "";
//        try {
//            result = ocrService.clovaOCRService(file);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return result;
//    }
//}

OCRService.java

//@Service
//public class OCRService {
//    public String clovaOCRService() { //clovaOCRService
//        String apiURL = "<https://k71crhiy0t.apigw.ntruss.com/custom/v1/22365/66a8cbf6b4216e8b6bfdd6b743b3c571e927d9ae489a6346e01490a4186d1813/document/receipt>";
//        String secretKey = "UURubUV6dmR0QW1aUG9hV0NIUGlpTEF6VFBmc1VZV2Y=";
//        String imageFile = "//C://Users//lg//Desktop//productImages//test4.jpg";
//        String result = "";
//        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("gi");
//            System.out.println(response); // API 호출 결과를 콘솔에 출력
//            // jsonToString() 메소드 호출하고 결과 받아옴
//            result = jsonToString(response.toString());
//            System.out.println(result);
//
//        } catch (Exception e) {
//            System.out.println("Hi");
//            System.out.println(e);
//
//        }
//        return result;
//    }