Where The Streets Have No Name

jsp 다운로드 이렇게 하면 됩니다. ( 한글도 됩니다. ) 본문

Developement/Java

jsp 다운로드 이렇게 하면 됩니다. ( 한글도 됩니다. )

highheat 2006. 4. 1. 01:41

<%@ page contentType="text/html; charset=8859_1"import="java.io.*" errorPage = "/error/error.jsp" %><%
String clips = new String((request.getParameter("clips")).getBytes("8859_1"),"EUC_KR");
String filename = clips; //파일 이름을 받는다.

 String path = getServletContext().getRealPath("/");
String other_path="upload/";

 File file = new File(path+other_path+"/"+filename); // 절대경로입니다.
response.setContentType("application/octet-stream");

String Agent=request.getHeader("USER-AGENT");
//response.setContentType("application/unknown");  //화일형태

 if(Agent.indexOf("MSIE")>=0){
int i=Agent.indexOf('M',2);//두번째 'M'자가 있는 위치
String IEV=Agent.substring(i+5,i+8);
if(IEV.equalsIgnoreCase("5.5")){
 response.setHeader("Content-Disposition", "filename="+new String(filename.getBytes("euc-kr"),"8859_1"));
}else{
 response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("euc-kr"),"8859_1"));
}
}else{
 response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("euc-kr"),"8859_1"));
}

 byte b[] = new byte[5 * 1024 * 1024];  //5M byte까지 업로드가 가능하므로 크기를 이렇게 잡아주었음.
if (file.isFile()){ 
try {
 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.flush();
 outs.close(); 
 fin.close();
}catch(Exception e){}
}

%>