| sitelink1 | |
|---|---|
| sitelink2 | |
| sitelink3 | |
| sitelink4 | |
| sitelink5 | |
| sitelink6 | 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadTest extends HttpServlet {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    doGet(req, res);
  }
  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    String id = req.getParameter("id");
    String pass = req.getParameter("pass");
    if (id.equals("down") && pass.equals("test")) {
      try {
        sendFile(req, res, "D:/TEST.xls");
      } catch (Exception e) {
        // Logger.err.println("File Download is failed : "+e);
      }
    } else {
      res.setContentType("text/html;charset=euc-kr");
      PrintWriter out = res.getWriter();
      out.println("<html><body><center>");
      out.println("invalid user information!!");
      out.println("</center></body></html>");
    }
  }
  /**
   * <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(HttpServletRequest request,
      HttpServletResponse response, String path) throws Exception {
    this.sendFile(request, response, new File(path));
  }
  /**
   * <pre>
   *      파일을 전송하는 메소드
   *      <b>response를 reset()하기 때문에 Cookie 변경 등의 작업을 수행해서는 안된다.</b>
   * </pre>
   */
  public void sendFile(HttpServletRequest request,
      HttpServletResponse response, 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());
        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 = "Content-type: application/x-ms-download";
      response.setContentType(contentType);
      response.setHeader("Content-Disposition", "attachment; filename=""
          + new String(f.getName().getBytes(), "Shift_JIS") + """);
      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) {
        }
    }
  }
}
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 | 
|---|---|---|---|---|
| 6 | HttpURLConnection을 사용하여 웹 페이지 액세스하기 (첫번째) | 황제낙엽 | 2006.12.28 | 716 | 
| » | Servlet으로 구현한 파일 다운로드(download) 페이지 | 황제낙엽 | 2006.12.27 | 624 | 
| 4 | JSP로 구현한 파일 다운로드(download) 페이지 | 황제낙엽 | 2006.12.27 | 2831 | 
| 3 | 파일 Download로직 구현시에 참고할만한 내용 | 황제낙엽 | 2006.12.27 | 677 | 
| 2 | 파일을 web 이외의 경로에 올린다음 보내는 코드 예제 (JDK1.3)   | 황제낙엽 | 2006.12.27 | 588 | 
| 1 | NestedRuntimeException | 황제낙엽 | 2006.09.22 | 1596 | 
 
							