2016년 10월 12일 수요일

[Spring] 이미지 보여주기 & 파일 다운로드

[Spring] 이미지 보여주기 & 파일 다운로드

이미지 보여주기

<img src="<c:url value='/web/file/attachImage.do?seq=${seq}&no=${no}'/>" width="550" />

img 태그를 삽입하고 이미지 데이터를 조회할 경로를 입력합니다.


@RequestMapping("/attachImage.do")
public void attachImage(RMap rmap, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {		
	UMap umap = fileService.selectAttachFile(rmap, model);
	byte[] imageData = (byte[]) umap.get("file");
	response.setContentType("image/jpeg");
	response.getOutputStream().write(imageData);
}

DB 에서 데이터를 조회합니다. (byte array)
contentType 을 image/jpeg로 설정합니다.
response 를 통해 결과를 전송합니다.

파일 다운로드

window.location="/web/file/fileDownload.do?seq="+seq+"&no="+no;

다운로드할 파일을 요청하는 URL를 설정합니다.


@RequestMapping("/fileDownload.do")
public void fileDownload(RMap rmap, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {		
	UMap umap = fileService.selectAttachFile(rmap, model);
	byte[] imageData = (byte[]) umap.get("file");
	String filename = (String) umap.get("attach_file_name");
	response.setContentType("application/octet-stream");
	response.setHeader("Content-Disposition", "attachment;filename=" + filename);
	response.getOutputStream().write(imageData);
}

DB 에서 데이터를 조회합니다. (byte array)
contentType 을 application/octet-stream로 설정합니다.
header 에 filename 을 설정합니다.
response 를 통해 결과를 전송합니다.

참고

http://egloos.zum.com/yooncom/v/9069964
http://hellogk.tistory.com/129

댓글 없음:

댓글 쓰기

[시스템 트레이딩] 종목 코드 얻기

[시스템 트레이딩] 종목 코드 얻기 사이보스플러스를 이용합니다. 기본 프로그램들을 모두 설치한 후에 CpUtil.CpCodeMgr 를 이용하여 종목 코드들을 얻을 수 있습니다. 야후 파이넨스를 사용하려다가 안되서, 사이보스를 활용하였습니다. 아래...