투케이2K

89. (TWOK/ALGORITHM) [Spring] 문법 - RestApi 사용해 로컬 서버에 저장된 텍스트 (text) 파일 호출 및 내용 표시 실시 본문

투케이2K 알고리즘

89. (TWOK/ALGORITHM) [Spring] 문법 - RestApi 사용해 로컬 서버에 저장된 텍스트 (text) 파일 호출 및 내용 표시 실시

투케이2K 2023. 1. 22. 12:55
반응형

[환경 설정 및 설명]

언어 / 플랫폼 : Java / Spring

설 명 : 문법 - RestApi 사용해 로컬 서버에 저장된 텍스트 (text) 파일 호출 및 내용 표시 실시

 

[소스 코드]


    // [GET 방식 : map]
    // [경로 지정 : http://localhost:7000/showText?file=test.txt]
    // [로직 : 서버 로컬 pc에 저장 된 텍스트 파일 >> 바이트 리턴 반환 >> 텍스트 내용 표시 실시]
    @GetMapping("/showText")
    public ResponseEntity<Resource> showText(@RequestParam Map<String, String> param) {
        System.out.println("\n");
        System.out.println("=======================================");
        System.out.println("[ModuleApiController] : [showText] : [start]");
        System.out.println("[request file] : " + String.valueOf(param.get("file")));
        System.out.println("=======================================");
        System.out.println("\n");

        // 시스템 os 정보 확인 변수 선언
        String os = System.getProperty("os.name").toLowerCase();


        // 사진이 저장된 폴더 경로 변수 선언
        String fileRoot = "";


        // os 정보 확인 및 사진이 저장된 서버 로컬 경로 지정 실시
        // 로컬 : Home/Resource/assets 폴더는 이미지 파일을 공통적으로 저장 관리
        if(os.contains("win")) {
            fileRoot = "c:/Home/Resource/assets/"; //윈도우 경로 (디스크 필요)
        }
        else if(os.contains("linux")) {
            fileRoot = "/Home/Resource/assets/"; //리눅스 경로
        }


        // 서버 로컬 경로 + 파일 명 저장 실시
        fileRoot = fileRoot + String.valueOf(param.get("file"));
        System.out.println("\n");
        System.out.println("=======================================");
        System.out.println("[ModuleApiController] : [showText] : [fileRoot]");
        System.out.println("[경로] : " + fileRoot);
        System.out.println("=======================================");
        System.out.println("\n");


        // Resorce를 사용해서 로컬 서버에 저장된 이미지 경로 및 파일 명을 지정
        Resource resource = new FileSystemResource(fileRoot);


        // 로컬 서버에 저장된 파일이 없을 경우
        if(!resource.exists()){
            System.out.println("\n");
            System.out.println("=======================================");
            System.out.println("[ModuleApiController] : [showText] : [Error]");
            System.out.println("[FILE] : " + "NOT_FOUND");
            System.out.println("=======================================");
            System.out.println("\n");
            return new ResponseEntity<Resource>(HttpStatus.NOT_FOUND); // 리턴 결과 반환 404
        }


        // 로컬 서버에 저장된 이미지가 있는 경우 로직 처리
        HttpHeaders header = new HttpHeaders();
        Path filePath = null;
        try {
            filePath = Paths.get(fileRoot);
            // 인풋으로 들어온 파일명 .png / .jpg / .txt 에 맞게 헤더 타입 설정
            header.add("Content-Type", Files.probeContentType(filePath));
        }
        catch (Exception e){
            e.printStackTrace();
        }
        // 파일 리턴 실시 [브라우저에서 get 주소 확인 가능]
        return new ResponseEntity<Resource>(resource, header, HttpStatus.OK);
    }
 

[결과 출력]

 

 

반응형
Comments