본문 바로가기
업무용/nexacro

바이너리 파일 다운로드(자바)

by SEOKIHOUSE 2023. 12. 28.
@RequestMapping(value = "/downLoadHaBscMttrPhoto.do")
    public void selectPhoto(HttpServletRequest request, HttpServletResponse response) throws NexacroException, ServletRequestBindingException, IOException {

    	String kornFlnm = ServletRequestUtils.getStringParameter(request, "kornFlnm");
    	String empno = ServletRequestUtils.getStringParameter(request, "empno");
    	HaBscMttrVo vo = haBscMttrService.selectByteImg(empno);

    	byte[] blobData = vo.getPhotoBt();// BLOB 형식의 이미지를 byte로 가져온다

        if (blobData == null) {
            return ;
        }

        String originalFilename = kornFlnm.replaceAll(" ", "");
        String filename = empno + "_" + originalFilename + ".jpg";
        // 파일 이름을 URL 인코딩 처리
        String encodedFilename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");

        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFilename);
        response.setContentLength(blobData.length);

        try (OutputStream outputStream = response.getOutputStream()) {
            outputStream.write(blobData);
            outputStream.flush();
        } catch (IOException e) {
        	e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }

    }