이거 원 만들때 마다 새로 만드니 당췌ㅡㅡ
Tomcat 4.1.30 + window XP SP2 단독 테스트
Tomcat 4.1.24 + Apache 2.0.43(맞나??) + linux 테스트.
IE 6.0 / Firefox 만 딱 두번씩 테스트 했네 ㅡㅡ
package com.javarush.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Tornado
*/
public class DownloadUtil {
private File file;
private HttpServletRequest req;
private HttpServletResponse res;
public DownloadUtil(){ }
public DownloadUtil(File file, HttpServletRequest req, HttpServletResponse res ){
this.file = file;
this.req = req;
this.res = res;
}
public void sendFile() throws Exception{
if(!(file.isFile() && file.exists())){
throw new Exception("file.not.found");
}
if(!initResponse()){
throw new Exception("download.failure");
}
FileInputStream fin = null;
FileChannel inputChannel = null;
WritableByteChannel outputChannel = null;
try{
fin = new FileInputStream(file);
inputChannel = fin.getChannel();
outputChannel = Channels.newChannel(res.getOutputStream());
inputChannel.transferTo(0, fin.available(), outputChannel);
}catch(Exception e){
throw e;
}finally{
try{
if(fin != null) fin.close();
if(inputChannel.isOpen()) inputChannel.close();
if(outputChannel.isOpen()) outputChannel.close();
}catch(Exception e){
}
}
}
public boolean initResponse() throws Exception{
if(res == null || req == null) return false;
if(file == null) return false;
res.setContentType("appliction/octet-stream");
res.setHeader("Accept-Ranges","bytes");
if (req.getHeader("User-Agent").indexOf("MSIE 5.5") > -1) {
// IE 6.0 Test Ok
res.setHeader("Content-Disposition", "filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8") + ";");
} else if(req.getHeader("User-Agent").indexOf("Mozilla/5.0") > -1){
// firefox Test Ok
if(req.getServerPort() == 8080){
res.setHeader("Content-Disposition", "filename=" + new String(file.getName().getBytes("KSC5601"), "ISO-8859-1") + ";");
}else{
res.setHeader("Content-Disposition", "filename=" + new String(file.getName().getBytes(), "KSC5601" ) + ";");
}
} else {
res.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8") + ";");
}
res.setContentLength((int)file.length());
return true;
}
}
사용법 :
<%@ page contentType="text/html; charset=euc-kr" import="java.io.*, java.net.*, com.javarush.util.*" %><%
request.setCharacterEncoding("euc-kr");
String fileName = request.getParameter("fileName");
if(fileName != null){
//fileName = URLDecoder.decode(fileName, "euc-kr");
// get 으로 넘어오건.. POST 로 넘어오건.. .
// DB 에서 파일이름을 읽어오건..
// 한글로 된 파일 이름이 콘솔에 제대로 나와야
// 다운로드가 제대로 됨
System.out.println("fileName : " + fileName);
File dir = new File(request.getContextPath() + "upload");
File file = new File(dir, fileName);
DownloadUtil downUtil = new DownloadUtil(file, request, response);
downUtil.sendFile();
}
%>
'JAVA > JSP_Servlet' 카테고리의 다른 글
[펌] http RFC (0) | 2005.01.17 |
---|---|
[펌]Single Sign On... (0) | 2005.01.17 |
헐.. 죽이넹 (0) | 2004.12.21 |
얼... 좋은걸~ (0) | 2004.12.17 |
Converting JDBC Result Sets to XML (0) | 2004.12.09 |