본문 바로가기

Server Dev

[JAVA/JSP] 쌩 JSP, 레알 초간단 파일 다운로드 소스[펌]

인터넷에 떠도는 '초간단 파일다운로드 소스'는 IBboard라는 라이브러리가 필요합니다.
그 소스는 개초보는 사용할 수 없습니다.
당신이 개초보라면 이 소스를 사용하는것이 정신건강에 좋습니다.

1. 소스에 '절대경로'를 실제 다운로드 받을 파일이 있는 위치로 수정하세요.
    윈도우라면 (C:\디렉토리\...), 리눅스라면 (/디렉토리/디렉토리/...)
2. 수정한 내용을 서버 루트에 'download.jsp'로 저장하십시요. (물론 이렇게 안해도 좋습니다.)
3. 브라우저를 키고 'http://서버주소/download.jsp?file=파일명' 으로 테스트하면 됩니다.
* 파일명은 서버에 존재해야합니다.

<%@ page contentType="application;" %>
<%@ page import="java.util.*,java.io.*,java.sql.*,java.text.*"%>
<%
String strFilename=java.net.URLDecoder.decode(request.getParameter("file"));
String strFilenameOutput=new String(strFilename.getBytes("euc-kr"),"8859_1");
File file=new File("절대경로"+strFilename);
byte b[]=new byte[(int)file.length()];
response.setHeader("Content-Disposition","attachment;filename="+strFilenameOutput);
response.setHeader("Content-Length",String.valueOf(file.length()));
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();
}
%>

이것도 안되면,
아래 댓글창에 질문을 올리거나,
머리를 쥐어뜯어버리세요.