hanker

Spring - local image 가져오기 본문

SPRING

Spring - local image 가져오기

hanker 2021. 4. 20. 17:30
  1. 이미지 파일은 각 날짜에 맞는 폴더에 저장
  2. 파일은 시간에 영향을 받아 저장( 파일명 - ex) 20210420_171013.050821_A.png )
  3. 특정시간에 생성된 이미지 파일 불러오기

<폴더 내 Image파일>

 

(파일명을 알 경우)

@RestController
public class ImageController {

    @GetMapping(value="/image/view", produces= MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody byte[] getImage(@RequestParam("file_time") String fileTime, // yyyymmdd_HHmmssZ
                                         @RequestParam("value") String value) // A
            throws IOException{
        FileInputStream fis = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        String[] fileAr = fileTime.split("_");
        String filePath = fileAr[0];

        String fileDir = "D:\\Han\\sample\\" + filePath + "\\" + fileTime + "_" + value + ".png"; // 파일경로

        try{
            fis = new FileInputStream(fileDir);
        } catch(FileNotFoundException e){
            e.printStackTrace();
        }

        int readCount = 0;
        byte[] buffer = new byte[1024];
        byte[] fileArray = null;

        try{
            while((readCount = fis.read(buffer)) != -1){
                baos.write(buffer, 0, readCount);
            }
            fileArray = baos.toByteArray();
            fis.close();
            baos.close();
        } catch(IOException e){
            throw new RuntimeException("File Error");
        }
        return fileArray;
    }
}

 

 

(파일명을 모르지만 특정시간을 입력해서 이미지를 얻고 싶을 때)

@RestController
public class ImageController {    
    
    @GetMapping(value="/image/specView", produces=MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody byte[] getSpecImage(@RequestParam("specific_date") String specDt, // yyyymmdd_HHmm (특정시간입력 s, ms까지는 모름)
                                             @RequestParam("value") String value) // A or B
            throws IOException{

        FileInputStream fis = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        String[] fileAr = specDt.split("_");
        String filePath = fileAr[0];
        String tmpFile = fileAr[1].substring(0, 4); // HHmm

        String path = "D:\\Han\\sample\\" + filePath;
        // path 경로에서 "filePath_tmpFile"명과 일치하는 파일 찾기
        File directory = new File(path);
        FilenameFilter filter = new FilenameFilter(){
            public boolean accept(File f, String name){
                return name.startsWith(filePath + "_" + tmpFile);
            }
        };

        String[] sameFile = directory.listFiles(filter)[directory.listFiles(filter).length - 1].toString().split(("/".equals(File.separator))? File.separator : "\\\\");
        String fileName = sameFile[sameFile.length-1];

        String fileDir = "D:\\han\\sample\\" + filePath + "\\" + fileName;
        try{
            fis = new FileInputStream(fileDir);
        } catch(FileNotFoundException e){
            e.printStackTrace();
        }

        int readCount = 0;
        byte[] buffer = new byte[1024];
        byte[] fileArray = null;

        try{
            while((readCount = fis.read(buffer)) != -1){
                baos.write(buffer, 0, readCount);
            }
            fileArray = baos.toByteArray();
            fis.close();
            baos.close();
        } catch(IOException e){
            throw new RuntimeException("File Error");
        }

        return fileArray;
    }
}
  • 선택한 특정 분에 제일 마지막 값이 들어옴 ex) 20210329-151059.999999_A.png
  • 더 detail한 설정이 필요하다면 초까지 설정

 

전체 코드

github.com/hanke-r/imageview

 

hanke-r/imageview

Contribute to hanke-r/imageview development by creating an account on GitHub.

github.com