À§·Î

JSP ¼Ò½º º¸±â - ±Û¾²±â

´ÙÀ½ÀÇ JSP ¼Ò½º´Â °Ô½ÃÆÇ¿¡¼­ ±Û¾²±â¿¡ ´ëÇÑ ¼¼ºÎ ¼Ò½ºÀÔ´Ï´Ù. ÀÌ ¼Ò½º¸¦ ±â¹ÝÀ¸·Î ±Û¾²±â ÀÔ·Â ÆûÀ» ¸¸µé¾î ¼Ò½º¿Í ¿¬°áÇØ ÁÖ¼Å¾ß °Ô½ÃÆÇÀÇ ±Û¾²±â°¡ Á¤»óÀûÀ¸·Î ÀÛµ¿ÇÕ´Ï´Ù.

±Û¾²±â ÇüÅÂÀÇ JSP ¼Ò½º

<%@ include file="board.conf.jsp" %>

<%@ include file="board.util.jsp" %>

<%@ page import="java.sql.*, com.jspsmart.upload.SmartUpload, java.io.File, java.io.FileOutputStream, com.namo.NamoMime" %>

<%@ page contentType="text/html" %>

<jsp:useBean id="myUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />

<%

/*------------------------------------------------------------------------

        ... (ÁÖ¼® »ý·«) ...

--------------------------------------------------------------------------*/

 

        String sql;

        String uploadPath;

        String uploadUrl;

        File makeDir = null;

        FileOutputStream outFile = null;

        int i = 0;

        int j = 0;

 

        int msgno = 0;

        int parentMsgno = 0;

        int pId = 0;

        int tId = 0;

        int tStep = 0;

        Connection con; 

        Statement stmt = null;

        ResultSet rs = null;

        int offset = Integer.parseInt(((request.getParameter("offset") == null ) ?  "0" : request.getParameter("offset")));

 

        // SmartUpload ÃʱâÈ­ - SmartUpload´Â jsp, servlet¿¡¼­ HTML Upload¸¦ ±¸ÇöÇØ ÁÖ´Â beanÀÔ´Ï´Ù.

        myUpload.init(config);

        myUpload.service(request,response);

        myUpload.setMaxFileSize(2097152);

        myUpload.upload();

 

        // JDBC µ¥ÀÌÅͺ£À̽º Ä¿³Ø¼ÇÀ» À§ÇÑ URLÀ» ¼³Á¤ÇÕ´Ï´Ù.

        String dbUrl = "jdbc:mysql://" + BBS_HOST + "/" + BBS_DB + "?user=" + BBS_USER + "&password=" + BBS_PASS;

 

        // JDBC Ŭ·¡½º¸¦ ÀоîµéÀ̰í, Statement¸¦ »ý¼ºÇÕ´Ï´Ù.

        Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 

        con = java.sql.DriverManager.getConnection(dbUrl);

        stmt = con.createStatement();

 

        // ÀÔ·ÂÇÒ °Ô½Ã¹°ÀÇ ¹øÈ£¸¦ »ý¼ºÇÕ´Ï´Ù.

        rs = stmt.executeQuery("select max(msgno) from board");

        rs.first();

        msgno = rs.getInt(1) + 1;

        rs.close();

 

        // ¾÷·ÎµåÇÒ À§Ä¡¿Í MIME¿¡¼­ ´ëÄ¡ÇÒ LINK¸¦ ¸¸µì´Ï´Ù.

        uploadPath = BBS_DATA_PATH + "/" + msgno + "/";

        uploadUrl = BBS_DATA_URL + "/" + msgno + "/";

 

        // ÆÄÀÏÀ» ¾÷·ÎµåÇϱâ

        makeDir = new File(uploadPath);              // ¾÷·ÎµåÇÒ µð·ºÅ丮 »ý¼º

        makeDir.mkdir();

        makeDir = null;

 

        // ¾÷·ÎµåÇÑ ÆÄÀÏ ÀúÀåÇϱâ

        for(i = 0; i < myUpload.getFiles().getCount(); i++)

        {

          // ¾÷·Îµå ÇÒ ÆÄÀÏÀÌ ÀÖÀ¸¸é

          if(!myUpload.getFiles().getFile(i).isMissing())        

          {

            outFile = new FileOutputStream(uploadPath +

                          myUpload.getFiles().getFile(i).getFileName());

            for(j = 0; j < myUpload.getFiles().getFile(i).getSize(); j++)

                   outFile.write((int)myUpload.getFiles().getFile(i).getBinaryData(j));

            outFile = null;

            sql = "insert into filelist ( msgno, filename ) values ( ";

            sql += msgno + ", ";

               sql += "'" + myUpload.getFiles().getFile(i).getFileName() + "') ";

                     stmt.executeQuery(sql);

          }

        }       

 

        // »ç¿ëÀÚ°¡ ÀÔ·ÂÇÑ °Ô½Ã¹° ³»¿ë °¡Á®¿À±â

        String author = myUpload.getRequest().getParameter("author");

        String email = myUpload.getRequest().getParameter("email");

        String homepage = myUpload.getRequest().getParameter("homepage");

        String subject = myUpload.getRequest().getParameter("subject");

        String msgbody = myUpload.getRequest().getParameter("contents");

        String passwd = myUpload.getRequest().getParameter("pass");

        String mode = request.getParameter("mode");

 

        // MIME µðÄÚµùÇϱâ Çϱâ

        NamoMime mime = new NamoMime();

        mime.setSaveURL(uploadUrl);

        mime.setSavePath(uploadPath);

        mime.decode(msgbody);                          // MIME µðÄÚµù

        mime.saveFile();                               // Æ÷ÇÔÇÑ ÆÄÀÏ ÀúÀåÇϱâ

        msgbody = replace(mime.getBodyContent(), "'", "\\'");

 

        // ¾²±â ¸ðµå°¡ ½Å±Ô ÀÛ¼ºÀÎ °æ¿ì

        if(mode.equals("write"))

        {

                // Äõ¸® ¸¸µé±â

                sql = "insert into board (msgno, wname, email, homepage, subject, msgbody, passwd, p_id, wtime) values (";

                sql += msgno + ", ";

                sql += "'" + author + "', ";

                sql += "'" + email + "', ";

                sql += "'" + homepage + "', ";

                sql += "'" + subject + "', ";

                sql += "'" + new String(msgbody.getBytes(), "iso-8859-1") + "', ";

                sql += "'" + passwd + "', ";

                sql += msgno + ", ";

                sql += "Now());";

                stmt.executeQuery(sql);

        }

        // ¾²±â ¸ðµå°¡ ½Å±Ô ÀÛ¼ºÀÌ ¾Æ´Ï¶ó ´äº¯ÀÎ °æ¿ì

        else if(mode.equals("reply"))   

        {

                parentMsgno = Integer.parseInt(request.getParameter("msgno"));

                // DB¿¡¼­ ¿ø¹®ÀÇ Á¤º¸¸¦ °¡Á®¿É´Ï´Ù

                sql = "select msgno, p_id, t_id, t_step from board where msgno =" + parentMsgno;               

                rs = stmt.executeQuery(sql);

                rs.first();

 

                pId = rs.getInt("p_id");

                tId = rs.getInt("t_id");

                tStep = rs.getInt("t_step");

                rs.close();

                // ÀÔ·ÂµÈ ´äº¯ ÀÌÈÄÀÇ °Ô½Ã¹°¿¡ ´ëÇØ¼­ ¾²·¹µå ¼ø¼­ ID¸¦ 1¾¿ Áõ°¡½Ãŵ´Ï´Ù

                sql = "update board set t_id = t_id + 1 where p_id = "+ pId + " and t_id >= " + (tId + 1);

                stmt.executeQuery(sql);

 

                // °Ô½Ã¹°À» DB ¿¡ ÀÔ·ÂÇÕ´Ï´Ù.

                sql = "insert into board (msgno, wname, email, homepage, subject, msgbody, p_id, t_id, t_step, wtime) values (";

                sql += msgno + ", ";

                sql += "'" + author + "', ";

                sql += "'" + email + "', ";

                sql += "'" + homepage + "', ";

                sql += "'" + subject + "', ";

                sql += "'" + new String(msgbody.getBytes(), "iso-8859-1") + "', ";

                sql += pId + ", ";

                sql += (tId + 1) + ", ";

                sql += (tStep + 1) + ", ";

                sql += "Now());";

                stmt.executeQuery(sql);

        }

 

        // JDBC ¿¬°á°ú Statement¸¦ ²÷½À´Ï´Ù.

        stmt.close();

        con.close();

        response.sendRedirect("list.jsp?offset=" + offset);

%>

 

 JSP Ãʱâ È­¸é | ±Û¾²±â ÀÔ·Â Æû