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);


번호 제목 글쓴이 날짜 조회 수
231 싱글톤 방식의 테스트용 Temporary Data Access Object 황제낙엽 2017.01.12 1603
230 SimpleDateFormat Symbol file 황제낙엽 2016.12.20 74
229 JSON-lib Java Library file 황제낙엽 2013.04.09 91
228 JSP 파일에서 getOutputStream() has already been called for this response 에러 황제낙엽 2013.04.24 11479
227 servlet 에서의 json 한글처리 황제낙엽 2013.04.23 1519
226 -file.encoding의 역할 (다국어, 한국어) 황제낙엽 2013.04.10 235
225 [The type HttpUtils is deprecated] javax.servlet.http.HttpUtils 황제낙엽 2013.03.20 276
224 com.oreilly.servlet.multipart 를 이용한 파일 업로드 file 황제낙엽 2013.03.19 104
223 String to InputSource 황제낙엽 2012.12.03 77
222 Class.getResource() vs. ClassLoader.getResource()/getResources() 황제낙엽 2012.06.24 57
221 Jar파일에 포함된 리소스 접근하는 방법(How to read a resource from a JAR file ) file 황제낙엽 2012.06.24 164
220 Java에서 URL 다루기 file 황제낙엽 2012.06.24 88
» HttpServletResponse.setContentType(java.lang.String type) 과 MIME 타입 황제낙엽 2012.04.20 172
218 code, codebase 속성과 applet object 동적 생성 file 황제낙엽 2012.04.17 85
217 한글 파일명 깨짐으로 살펴본 다국어 처리 문제 (UTF-8) 황제낙엽 2012.03.22 10121
216 BufferedReader.readLine() hangs 황제낙엽 2012.02.23 520
215 OS 쉘 명령어(shell script) 실행하기 [ProcessBuilder, Runtime.getRuntime().exec()] 황제낙엽 2012.02.22 664
214 PreProcess 실행 (전처리기 만들기) file 황제낙엽 2012.01.04 169
213 javax.script와 타입변환 황제낙엽 2012.01.03 62
212 Scripting within Java 황제낙엽 2012.01.03 69