모두모두 개발자다요/JAVA

이미지 파일 다운로드

鬼미쿠 2009. 3. 30. 16:42

맨날 한다는게 그저 DB에 쿼리 때려서 결과 받아 뿌리기만 줄줄하다보니,
이런건 구경도 못해보고 만져보지도 못하고 살다가 이제야 써먹어봤다.

<%@ page contentType="application; charset=EUC-KR"  import="java.io.*, java.text.*"%>
<%
           String filename = request.getParameter("filename");
           String path = request.getParameter("path");
           String afname = path+filename;    //파일 들어가있는 경로+파일명(뭐 대충 알기로는 절대경로..)
           
           /*********************************************************
            * HTTP 헤더 세팅
            **********************************************************/
           response.reset();
           
           if(request.getHeader("User-Agent").indexOf("MSIE5.0") > -1){
            // IE가 아닐 경우
             response.setHeader("Content-Type", "doesn/matter;");
             response.setHeader("Content-Disposition", "attachment;filename=\""+filename + "\"");
           }else{
            // IE일 경우
             response.setHeader("content-type","application/unknown");
             response.setHeader("Content-Disposition", "attachment;filename=\""+filename + "\"");
           }

            
            /*********************************************************
            * 파일 다운로드
            **********************************************************/
          
           File fp = new File(afname);
           int read=0;

           byte b[]=new byte[(int)fp.length()];    //파일 크기

           if(fp.isFile()){

             BufferedInputStream fin = new BufferedInputStream(new FileInputStream(fp));
             BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());

             //파일 읽어서 브라우져로 쓰기
             try{
               while((read=fin.read(b))!=-1){
               outs.write(b,0,read);
            }
               outs.close();
               fin.close();
            
             }catch(Exception e){
                  out.println(e);
             }finally{
                  if(outs!=null){outs.close();}
                  if(fin != null){fin.close();}
             }
           }
%>



반응형