파일 업로드/다운로드와 파일 삭제

파일 업로드/다운로드와 파일 삭제

파일 업로드

http://blog.naver.com/bb_/220323686927

다운로드 링크거는 부분

<jsp:useBean id=”dao” class=”com.databoard.dao.DataBoardDAO”/>
<%
 String strNo=request.getParameter(“bno”);
 String strPage=request.getParameter(“page”);
 
 DataBoardDTO d=dao.boardContentData(Integer.parseInt(strNo));

   .

(중략)

   .

   .

<a href=”../databoard/download.jsp?fn=<%=d.getFilename()%>”><%=d.getFilename()%></a>(<%=d.getFilesize() %>Bytes)

파일 다운로드

<%@ page language=”java” contentType=”text/html; charset=EUC-KR”
    pageEncoding=”EUC-KR”%>
<%@ page import=”java.io.*”%>
<%@ page import=”java.net.*”%>
<%
 String fn=request.getParameter(“fn”);
 String path=”c:\\download\\”;
 File file=new File(path+fn);
 //셋 헤더
 response.setHeader(“Content-Disposition”, “attachment;filename=”+URLEncoder.encode(fn,”UTF-8”));
                 //fn을 보내는데 UTF-8로 보낸다
                 //euc-kr은 불가능하다
 response.setContentLength((int)file.length());
 
 BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
 BufferedOutputStream bos=new BufferedOutputStream(response.getOutputStream());
                                                   //접속한 클라이언트의 영역
 
    byte[] buffer=new byte[1024];//한번 읽을때마다 1024씩
 int i=0;
    while((i=bis.read(buffer,0,1024))!=-1){
     //-1이 EOF.
     bos.write(buffer,0,i);
    }
    bis.close();
    bos.close();
    out.clear();
    out=pageContext.pushBody();//out객체를 다시 가져온다
%>

파일 삭제

<%@ page language=”java” contentType=”text/html; charset=EUC-KR”
    pageEncoding=”EUC-KR” import=”com.databoard.dao.*”%>
<%@ page import=”java.io.*” %>
<jsp:useBean id=”dao” class=”com.databoard.dao.DataBoardDAO”/>
<%
 String strNo=request.getParameter(“no”);
 String strPage=request.getParameter(“page”);
 String pwd=request.getParameter(“pwd”);
 //DB 연동
 String fn=dao.databoardGetFileName(Integer.parseInt(strNo));
 boolean bCheck=dao.databoardDelete(Integer.parseInt(strNo), pwd);
 
 if(bCheck==true){
  //파일 삭제
  File file=new File(“c:\\download\\”+fn);
  file.delete();
  
  //이동
  response.sendRedirect(“../main/main.jsp?no=7&page=”+strPage);
  
 }else{
 %>
  <script>
  alert(“비밀번호가 틀립니다”);
  history.back();
  </script>
 <%
 }
%>

관련 DAO
 //삭제
 public boolean databoardDelete(int no,String pwd){
  boolean bCheck=false;
  try{
   getConnection();
   String sql=”SELECT pwd FROM databoard WHERE no=?”;
   ps=conn.prepareStatement(sql);
   ps.setInt(1, no);
   ResultSet rs=ps.executeQuery();
   rs.next();
   String db_pwd=rs.getString(1);
   rs.close();
   ps.close();
   
   if(db_pwd.equals(pwd)){
    bCheck=true;
    sql=”DELETE FROM databoard WHERE no=?”;
    ps=conn.prepareStatement(sql);
    ps.setInt(1, no);
    ps.executeUpdate();//삭제
    ps.close();
   }
   else
   {
    bCheck=false;
   }
  }
  catch(Exception ex){System.out.println(ex.getMessage());}
  finally{
   disConnection();
  }
  return bCheck;
  
 }
 public String databoardGetFileName(int no){
  String result=””;
  try{
   getConnection();
   String sql=”SELECT COUNT(filename) FROM databoard WHERE no=?”;//파일 올렸었는지 안올렸었는지 확인
   ps=conn.prepareStatement(sql);
   ps.setInt(1, no);
   ResultSet rs=ps.executeQuery();
   rs.next();
   int count=rs.getInt(1);
   rs.close();
   ps.close();
   if(count==0){
    //파일이 있다면
    result=”NOFILE”;
   }
   else{
    //파일이 없다면
    sql=”SELECT filename FROM databoard WHERE no=?”;
    ps=conn.prepareStatement(sql);
    ps.setInt(1, no);
    rs=ps.executeQuery();
    rs.next();
    result=rs.getString(1);
    rs.close();
   }
   
   
  }catch(Exception ex){}
  finally{
   disConnection(); 
  }
  return result;
 }