sitelink1 http://blog.naver.com/shreds?Redirect=Log&logNo=89747030 
sitelink2 http://www.iana.org/assignments/media-types/text/index.html 
sitelink3  
sitelink4  
sitelink5  
sitelink6  

1. setContentType 메서드

public void setContentType(java.lang.String type)
Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response's character encoding is only set from the given content type if this method is called before getWriter is called.

This method may be called repeatedly to change content type and character encoding. This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.

Parameters:
type - a String specifying the MIME type of the content


2. MIME Type

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


마지막에 굵게 표기되어 있는 octet-stream 이라는 놈은 이름 그대로 8비트 바이너리 배열을 의미하며 http나 이메일상에서 application 형식이 지정되지 않았거나 형식을 모를때 사용합니다. 결국 브라우저는 octet-stream 으로 MIME 타입이 지정된 경우 단지 바이너리 데이터로서 다운로드만 가능하게 처리하게 됩니다.


※ setContentType 메소드로 contentType을 지정해 놓고 출력 스트림을 바꾸는 것은 물건을 줬다 뺐는 행위입니다.
즉 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);


번호 제목 글쓴이 날짜 조회 수
43 세션의 timeout 설정 >> HttpSession.setMaxInactiveInterval() 황제낙엽 2019.07.03 8649
42 jQuery JSON 데이터 통신의 특성 (HttpServletRequest) 황제낙엽 2019.06.23 581
41 [HttpURLConnection] 서버와의 통신 시도 시점 관련 황제낙엽 2019.06.23 650
40 응답 헤더의 Content-disposition 속성 황제낙엽 2019.04.16 902
39 Java에서 User-Agent 파써 사용하기 황제낙엽 2017.11.20 843
38 서버구동시 주기적으로 동작을 수행하는 스레드를 함께 실행하는 서블릿 황제낙엽 2017.08.02 551
37 JSP 파일에서 getOutputStream() has already been called for this response 에러 황제낙엽 2013.04.24 11973
36 [The type HttpUtils is deprecated] javax.servlet.http.HttpUtils 황제낙엽 2013.03.20 628
35 Java에서 URL 다루기 file 황제낙엽 2012.06.24 454
» HttpServletResponse.setContentType(java.lang.String type) 과 MIME 타입 황제낙엽 2012.04.20 576
33 한글 파일명 깨짐으로 살펴본 다국어 처리 문제 (UTF-8) 황제낙엽 2012.03.22 10508
32 <jsp:include>, include지시자 file 황제낙엽 2011.07.24 498
31 <jsp:include> 액션태그 황제낙엽 2011.07.24 451
30 Servlet의 각종 Listener 사용방법 및 샘플 황제낙엽 2010.10.26 450
29 ServletContext 초기화 및 소멸 황제낙엽 2010.10.26 461
28 ServletConfig 이용하기 황제낙엽 2010.03.15 521
27 Transfer-Encoding: chunked VS Content-Length 황제낙엽 2009.09.17 573
26 서블릿 응답 헤더(Response Header) 황제낙엽 2009.09.17 524
25 MultipartRequest (cos.jar)와 서블릿을 이용한 업로드 file 황제낙엽 2009.06.19 713
24 JSP session 정보 얻기 황제낙엽 2009.01.21 492