본문 바로가기

Study/Programming

JSP 파일 업로드

board_write1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
    String searchkey = "";
    if(request.getParameter("searchkey") != null) {
        searchkey = request.getParameter("searchkey");
    }
    String searchword = "";
    if(request.getParameter("searchword") != null) {
        searchword = request.getParameter("searchword");
    }
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
<script language='javascript'>
<!--
    String.prototype.trim = function() {
        return this.replace(/(^\s*)|(\s*$)/gi, "");
    }

    function ChkForm(form) {
        if(document.wfrm.subject.value.trim() == "") {
            window.alert("제목을 입력하세요");
            return false;
        }
        if(document.wfrm.writer.value.trim() == "") {
            window.alert("이름을 입력하세요");
            return false;
        }
        if(document.wfrm.passwd.value.trim() == "") {
            window.alert("암호를 입력하세요");
            return false;
        }
    }
//-->
</script>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <form action='board_write_ok1.jsp' method='post' name='wfrm' onSubmit='return ChkForm(this)' enctype='multipart/form-data'>
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='80' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>제목</font> :
            </td>
            <td>
                <input type='text' name='subject' size='60' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='80' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' size='10' maxlength='10' class='form'>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='email' size='20' maxlength='70' class='form'>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<input type='password' name='passwd' size='10' maxlength='10' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='80' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>내용</font> :
            </td>
            <td>
                <textarea name='content' style='width:620;height:300' class='form'></textarea>
            </td>
        </tr>
<!--  파일 업로드  -->
        <tr>
            <td width='80' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>내용</font> :
            </td>
            <td>
                <input type='file' name='upload' size='30'>
            </td>
        </tr>
       
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>

        <table width='100%' cellpadding='0' cellspacing='0' border='0'>
        <tr>
            <td width='500' height='30'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='../images/btn_wri.gif' border='0'>&nbsp;
                <a href='board_list1.jsp?searchkey=<%=searchkey %>&searchword=<%=searchword %>'><img src='../images/btn_list.gif' border='0'></a>
            </td>
        </tr>
        </table>
        </form>
    </td>
</tr>
</table>       
       
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


board_write_ok1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>   
<%
    String strPath="/home/whitefox/upload";
    int maxFilesize = 2*1024*1024;
    String strEncoding = "utf-8";
    MultipartRequest mr = new MultipartRequest(request, strPath, maxFilesize, strEncoding, new DefaultFileRenamePolicy());
   
    //일반적인 데이터 값   
    out.println(mr.getParameter("writer") +"<br>");
    //동일한 파일명 업로드에 대한 내용
    out.println(mr.getFilesystemName("upload") +"<br>"); //바뀐 이름
    out.println(mr.getOriginalFileName("upload") +"<br>"); //본래 이름
    out.println(mr.getFile("upload").length() +"<br>"); //파일의 크기
    %>


'Study > Programming' 카테고리의 다른 글

jsp 회원가입폼 만들기  (0) 2009.06.04
JSP 쿠키  (0) 2009.06.04
jsp에서 파일 업로드 처리  (0) 2009.06.03
JSP 게시판 공백처리  (0) 2009.06.01
JSP 게시판  (0) 2009.05.29