자바 웹 다운로드

JAVA/자바 2016. 10. 20. 11:36

public void chartRiskStatusDownload(HttpServletRequest request , HttpServletResponse response) throws IOException, AWTException{

Map<String, Object> rtnMap = new HashMap<String, Object>();

String filename = request.getParameter("filename");

String path = request.getParameter("path");

/** HTTP 헤더 셋팅 */

// response.reset();

 

// IE 체크

response.setContentType("application/octet-stream;charset=utf-8");

response.setCharacterEncoding("UTF-8");

response.setHeader("Content-Disposition", "attachment;filename=" + filename);

response.setHeader("Content-Transfer-Encoding", "binary;");

 

/** 파일 다운로드 */

LOGGER.info(String.format(LogFormat.DEBUG,"filename "+filename+" filepath "+path));

    Robot robot = new Robot();

    

        //모니터 화면 크기 가지고 오는 객체

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

 

        //모니터의 화면을 selectRect에 가로,세로 표현

        Rectangle selectRect = new Rectangle((int)screen.getWidth(), (int)screen.getHeight());

        

        //스크린샷된 데이터를 bufferedimage에 담음

        BufferedImage buffimg = robot.createScreenCapture(selectRect);

 

        

        //bufferedimage to inputstream convert

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ImageIO.write(buffimg, "png", baos);

        InputStream is = new ByteArrayInputStream(baos.toByteArray());

        

        //output을 위해 bufferedimage를 byte데이터로 바꿈

        byte[] imageBytes = baos.toByteArray();

        

int read = 0;

BufferedInputStream fin =

new BufferedInputStream(is);

BufferedOutputStream outs =

new BufferedOutputStream(response.getOutputStream());

// 파일 읽어서 브라우저로 출력하기

try {

       while((read=fin.read(imageBytes)) != -1){

        LOGGER.info(String.format(LogFormat.DEBUG,"fileread "+read));

         outs.write(imageBytes, 0, read);

       }

} catch (Exception e) {

e.printStackTrace();

} finally{

if (outs != null) {outs.close();}

       if (fin != null) {fin.close();}

  }

}

}

블로그 이미지

왕왕왕왕

,