2012년 6월 21일 목요일

[jsp] img태그 src에 한글명 넣기(톰캣)



1. html
   <img src = "filedownload.jsp?filename=사진.jpg">




2. filedownload.jsp
-----------------------------------------------------------

<%@page contentType="text/html; charset=euc-kr" import ="java.io.*,java.text.*,java.util.*"%>


<%


String filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"euc-kr");
String filename1 = java.net.URLEncoder.encode(filename, "UTF8");


System.out.println(filename);
System.out.println(filename1);


System.out.println("Request File Name:"+filename);
String appRoot = request.getRealPath("/");
System.out.println(appRoot);


File file = new File (appRoot+"/upload/"+filename);


System.out.println("DownLoad File:"+ file.getPath());


if(!file.exists()) {


%>


<script type="text/javascript">
alert("요청한 파일을 찾을 수 없습니다..");
history.back();
</script>
<%


  return;


}


BufferedInputStream input = null; 
BufferedOutputStream output = null;


response.reset();


String strClient = request.getHeader("User-Agent");
if (strClient.indexOf("MSIE 5.5") > -1) {  //익스  5.5
response.setContentType("application/x-msdownload"); 
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-Disposition", "filename=" + filename1 + ";"); 
response.setHeader("Content-Length", ""+ file.length());
   }else{           //익스 5.5이상
response.setContentType("application/x-msdownload"); 
response.setHeader("Content-Disposition", "attachment; filename=\""+filename1+"\";");
response.setHeader("Content-Transfer-Encoding", "binary;");
response.setHeader("Content-Length", ""+ file.length());
}


try {
input = new BufferedInputStream( new FileInputStream(file) ); 
byte buffer[] = new byte[1024];
int len = 0;
out.clear();
out.flush();
output = new BufferedOutputStream(response.getOutputStream());
while((len = input.read(buffer)) > 0 ){
output.write(buffer,0,len);
}
} catch(Exception e) {
e.printStackTrace();
System.out.println("File DownLoad Exception:" + e.getMessage());
} finally{
response.flushBuffer();
if(input !=null)try{input.close();}catch(Exception e){}
if(output !=null)try{output.close();}catch(Exception e){}
}
%>
-----------------------------------------------------------

댓글 없음:

댓글 쓰기