FrameWork/Spring

[SPRING] FTP서버의 이미지 프리뷰

밍글링글링 2017. 8. 28.
728x90

MAPPER에서 DB 받아서 [CONTROLLER]

@RequestMapping(value="/imgPreview")
    public void preview(HttpServletResponse response, long id) throws Exception {
        Map<String, Object> map= new HashMap<>();
        map.put("id", id);
        Map<String, Object> commonFile= commonService.selectFile(map);

        if (commonFile == null)
            return;

        MediaType mediaType = null;
        String imagePath = "";

        switch ((String)commonFile.get("EXTENSION")) {
            case "JPEG":
            case "JPG":
                mediaType = MediaType.IMAGE_JPEG;
                break;

            case "PNG":
                mediaType = MediaType.IMAGE_PNG;
                break;

            case "GIF":
                mediaType = MediaType.IMAGE_GIF;
                break;

            default:
        }

        imagePath = "/data/fileserver/"+commonFile.get("RECEIPT_IDX")+"/"+(String) commonFile.get("SAVE_NM");

        if (mediaType != null) {
            byte[] bytes = FileUtils.readFileToByteArray(new File(imagePath));

            response.setContentType(mediaType.toString());
            response.setContentLength(bytes.length);
            IOUtils.copy(FileUtils.openInputStream(new File(imagePath)), response.getOutputStream());
        }
    }

 

 

[HTML]

<img src="/imgPreview?id=???">

 

 

728x90

댓글