728x90

보통 파일 다운로드와 크게 다르지 않으나 한글로 된 파일일 경우 다운로드 되지 않는 경우가 종종 발생한다.
==================download_image.jsp==========================================================
<%@ page contentType="application/octet-stream;charset=euc-kr" %>
// contentType 은 위와 같이 설정 한다.
//include 된 파일에 다른 컨텐츠타입이 선언 되어있을 경우 에러가 생기니 주의.
<%


response.setHeader("Content-Type","image/png"); //다운로드할 파일 형식 결정.

String filename = java.net.URLDecoder.decode(request.getParameter("file")); // 요청페이지 에서 넘어온 파일 경로

String dFileName= "test.png";

String filename2 = new String(filename.getBytes("8859_1"),"euc-kr"); // 파일 이름이 한글일 경우를 처리.
String path = "c:\";
File file = new File(path+filename2);
byte b[] = new byte[(int)file.length()];

System.out.println( "size : " + b.length);
System.out.println( "filename2 : " + filename2);
System.out.println( "path : " + path);

response.setHeader("Content-Disposition", "attachment;filename=" + new String(dFileName.getBytes("euc-kr")) + ";");
if (file.isFile())
{
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());
int read = 0;
while ((read = fin.read(b)) != -1){
outs.write(b,0,read);
}
outs.close();
fin.close();
}
%>

728x90

'JAVA' 카테고리의 다른 글

정규식 regular expression  (0) 2012.07.29
한글문제  (0) 2012.07.29
JavaScript 키코드  (0) 2012.07.29
JDK 설치  (0) 2012.07.29
JSP 쿠키, 세션  (0) 2012.07.29

+ Recent posts