jsp 자료실 소스

jsp 자료실 소스

content.jsp(내용보기 jsp)

<tr>
    <td width=20% align=center>첨부파일</td>
   <c:if test=”${vo.filesize!=0}”>
    <td colspan=”3″ align=left>
      <a href=”databoard/download.jsp?fn=${vo.filename}”>
     ${vo.filename}
      </a>
      &nbsp;(${vo.filesize} bytes)
    </td>
   </c:if>
   <c:if test=”${vo.filesize==0}”>
    <td colspan=”3″ align=left>첨부파일 없음</td>
   </c:if>
   </tr>

sql

 <select id=”databoardContentData” parameterType=”int” resultType=”DataBoard”>
  SELECT no,name,subject,content,regdate,hit,filename,filesize FROM databoard WHERE no=#{no}
 </select>

DAO

public static DataBoardVO databoardContentData(int no){
  return ssf.openSession().selectOne(“databoardContentData”,no);
 }

download.jsp부분(다운로드 jsp)

<%@ page language=”java” contentType=”text/html; charset=EUC-KR”
    pageEncoding=”EUC-KR”%>
<%@ page import=”java.io.*,java.util.*,java.net.*”%>
<%
 String fn=request.getParameter(“fn”);
    try{
     response.setHeader(“Content-Disposition”, “attachment;filename=”+URLEncoder.encode(fn, “UTF-8”));//다운로드 창 띄우기
     File file=new File(“c:\\download\\”+fn);

     response.setContentLength((int)file.length());//파일 크기 알려주기
     
  BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));//파일을 서버에서 읽기 시작함
  BufferedOutputStream bos=new BufferedOutputStream(response.getOutputStream());//리스폰스가 접속한 사람임. 접속한 사람에게 응답하기 위해서 리스폰스를 씀. 읽어온 파일을 접속한 사람에게 줌.
  
  int i=0;
  byte[] buffer=new byte[1024];//한번 읽을때마다 1024씩 읽는다.
  while((i=bis.read(buffer,0,1024))!=-1)
  {
   //-1 EOF
   bos.write(buffer,0,i);//i가 읽은 바이트 수
  }
  bis.close();
  bos.close();
  out.clear();
  out=pageContext.pushBody();//새로운 아웃객체를 생성해야 오류가 안남.
  
    }catch(Exception ex){
   
    }
%>