sitelink1 http://www.javaservice.net/~java/bbs/rea...1030426589 
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

 

보안 문제로 인해, jsp 파일등을 올리는 경우, 웹 경로에 올라가면 실행 권한을 얻게 되는
위험성이 있습니다. 또한 사용자의 권한에 따라서 파일 접근을 제한하고 싶은 경우에도
URL이 노출된 첨부파일의 경우 어려움이 있습니다.

따라서 이를 피하기 위해, web 경로 밖에 첨부파일을 올려놓고 jsp에서 직접 파일을
전송하는 방법을 사용해 왔는데, 참고가 될까해서 관련 함수의 예를 올립니다.
간단히 사용하시기 편리할 듯합니다.

p.s. 본 코드와 클래스는 JBE Framework에서 사용하는 클래스의 소스 코드로 
"About Java Web Programming" (영진출판사)와 http://www.freechal.com/jbe  사이트에서
정보를 얻을 수 있습니다.

==================
/**
* <pre>
* 파일을 전송하는 메소드
* <b>response를 reset()하기 때문에 Cookie 변경 등의 작업을 수행해서는 안된다.</b>
* </pre>
*/
public void sendFile(File f)
    throws Exception
{
    
    InputStream in = null;
    OutputStream out  = null;
    try{
        //File f = new File(path);
        if(!f.exists() || f.isDirectory()){
            Logger.err.println("File Not Exists :"+f.getAbsolutePath());
            //redirect("/jbe/dance_content.jsp");
            return;
        }
        Logger.debug.println("Download File:"+f.getAbsolutePath());
        in = new FileInputStream(f);
        
        // 쿠키 등의 정보를 지우므로 이전에 세팅을 해서는 안된다.
        response.reset() ;
        //Logger.debug.println("f.getName():"+f.getName());
        
        Logger.debug.println("FileDonwload called:"+f.getName());
        String contentType = null;
        try{
            java.util.Properties config = Configuration.getProperties();
            String MIME_FILE = config.getProperty("jbe.mimetype.file.location");
            Logger.debug.println("MIME file location:"+MIME_FILE);
            contentType = 
                new javax.activation.MimetypesFileTypeMap(MIME_FILE).getContentType(f);
        }catch(Exception e){
            Logger.debug.println("MIME 정보를 얻지 못하였습니다.:"+e.getMessage());
        }
        if(contentType ==null || contentType.equals("")){
            contentType = "unknown/octet-stream";    
        }
        Logger.debug.println("Content Type:"+contentType);

        response.setContentType(contentType) ;    
        response.setHeader ("Content-Disposition", "attachment; filename="" + 
        new String(f.getName().getBytes(),"ISO8859_1")+""" );
        //f.getName()+""" );
        response.setHeader ("Content-Length", ""+f.length() );

        out = response.getOutputStream();
        byte[] dd = new byte[4096];
        int leng = 0;
        while( (leng = in.read(dd)) > 0 ){
            out.write(dd,0,leng);
        }
        
    }finally{
        if(in !=null)try{in.close();}catch(Exception e){}
        if(out !=null)try{out.close();}catch(Exception e){}
    }
}

/**
 * <pre>
 * 파일을 전송하는 메소드. <br>
 * 파일의 절대 경로를 인자값으로 받아 파일을 전송한다. <br>
 * String s = "c:/jdk1.3.1/bin/javac.exe";
 * sendFile(s);
 * </pre>
 * @param  String path - 파일의 절대 경로
 * @return void 
 * @exception java.lang.Exception
 */

public void sendFile(String path)
    throws Exception
{
    this.sendFile(new File(path));
} 


==================

------------------------------
http://www.riceman.co.kr

JBE Community
http://www.freechal.com/jbe
------------------------------