Web/JSP
JSP setContentType 메소드와 MIME 타입
Request
2012. 4. 23. 20:05
[JSP setContentType 메소드와 MIME 타입]
setContentType(String) : MIME 타입을 지정합니다. 캐릭터의 인코딩을 지정할 수도 있습니다. | |
예) response.setContentType("text/xml"); // MIME 타입만 지정 response.setContentType("text/xml;charset=utf-8"); // MIME 타입 지정, 캐릭터의 인코딩 지정
쉽게 말해서 웹서버는 브라우저로 전송될 페이지가 html 인경우 text/html을 표준 MIME 타입으로 지정합니다.
그러나 필요에 의해서 이 MIME 타입을 변경하고자 할 경우나 또는 캐릭터의 인코딩셋을 변경하고자 할때 setContentType 메소드를 사용할 수 있습니다. 브라우져는 이 MIME 타입을 확인하고 어떤 파일의 스트림(stream)인 줄 알 수 있습니다.
MIME 타입들...
text/html audio/mpeg image/bmp image/jpeg application/pdf application/java application/jar application/x-zip application/msword application/msaccess application/vnd.ms-excel application/vnd.ms-powerpoint application/octet-stream | |
위의 MIME 타입에서 맨 마지막에 굵게 표기되어 있는 octet-stream 이라는 놈은 이름 그대로 8비트 바이너리 배열을 의미하며 http나 이메일상에서 application 형식이 지정되지 않았거나 형식을 모를때 사용합니다. 결국 브라우저는 octet-stream 으로 MIME 타입이 지정된 경우 단지 바이너리 데이터로서 다운로드만 가능하게 처리하게 됩니다.
즉, setContentType 메소드가 제일 먼저 호출되고 getOutputStream과 같은 출력 스트림 메소드가 사용되어져야 합니다.
response.setContentType("application/octet-stream");
//... 중간생략 //... 중간생략
byte[] bytestream = new byte[(int)file.length()]; filestream = new FileInputStream(file);
int i = 0, j = 0; while((i = filestream.read()) != -1) { bytestream[j] = (byte)i; j++; } outStream = response.getOutputStream(); outStream.write(bytestream); | | |
출처 :
http://www.webmadang.net/develop/develop.do?action=read&boardid=1004&page=1&seq=107