[톰캣_JSP]web upload test

프로젝트 2017. 1. 3. 14:41

원격지에서 자기 PC(고정IP 가 있어야 겠지요)로 파일을 전송해야 될때 유용하게 사용할수 있습니다.


(저는 그렇게 사용중입니다...^^;)


만든지 몇년이 지난상태라서 정확히 기억은 안나지만 아마도

아래 url 인듯 싶네요..

참고 : https://www.tutorialspoint.com/jsp/jsp_file_uploading.htm



tomcat-6.0.10_web_upload_test.zip

web_upload_test.zip





[첨부파일 설명 및 사용법]


1. 톰캣 설정


1) C:\tomcat-6.0.10_web_upload_test\bin 폴더에서 start.bat 파일 세팅 수정

(각자 환경에 맞게 수정하세요!!!)


[start.bat]

...

rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem
rem $Id: startup.bat 467182 2006-10-23 23:47:06Z markt $
rem ---------------------------------------------------------------------------

set CATALINA_HOME="C:\tomcat-6.0.10_web_upload_test"
set JAVA_HOME="C:\java\jdk1.6.0_16"
...





2) C:\tomcat-6.0.10_web_upload_test\conf\ 폴더에서 server.xml 파일 수정 ...포트 설정

[server.xml]

...

  <Service name="Catalina">

    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector connectionTimeout="20000" maxThreads="500" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="200" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

...





3) C:\tomcat-6.0.10_web_upload_test\conf\Catalina\localhost\ 폴더에 web_upload_test.xml 파일 생성

[web_upload_test.xml]

<Context path="/web_upload_test" docBase="C:\web_upload_test" debug="0" reloadable="true"  swallowOutput="true">

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
   
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
 <Logger className="org.apache.catalina.logger.FileLogger" prefix="stdout_" suffix=".log" timestamp="true"/>

    -->
</Context>



-------------------------------------------------------------------------------------------------------------------------

2. web_upload_test  설정


1) C:\ 에 web_upload_test 폴더 생성


2) C:\web_upload_test\ 폴더에 fileSelect.jsp 파일 생성

[fileSelect.jsp]

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR" %>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
  <title>파일 업로드를 심플하게 해보아요 :-)</title>
 </head>
 <body>
  <!-- enctype="multipart/form-data" 파일이나 대용량 데이터 보낼때 데이터 전송 방식 -->
   <form name="fileForm" method="post" enctype="multipart/form-data" action="fileUpload.jsp">
    작성자 : <input type="text" name="name"><br>
    제목 : <input type="text" name="subject"><br>
    파일명 : <input type="file" name="filename"><br>
    <input type="submit" value="파일올리기"><br>
   </from>
 </body>
</html>


3) C:\web_upload_test\ 폴더에 fileUpload.jsp 파일 생성

[fileUpload.jsp]

<%@ page language="java" contentType="text/html; charset=EUC-KR" %>
 <!-- 파일 업로드 처리를 위한 MultipartRequest 객체를 임포트 -->
<%@ page import="com.oreilly.servlet.MultipartRequest" %>

 <!-- 파일 중복처리객체 임포트 -->
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>

<%@ page import="java.util.*" %>

<%
   String uploadPath ="c:\\";
   int size = 1000*1024*1024; //업로드 파일 최대 크기 지정
   String name="";
   String subject="";
   String filename="";

   try{
       //파일 업로드. 폼에서 가져온 인자값을 얻기 위해 request 객체 전달,
       //업로드 경로, 파일 최대 크기, 한글처리, 파일 중복처리
       MultipartRequest multi = new MultipartRequest(request, uploadPath, size, "euc-kr",
           new DefaultFileRenamePolicy());

       //폼에서 입력한 값을 가져옴
       name = multi.getParameter("name");
       subject = multi.getParameter("subject");

       //업로드한 파일들을 Enumeration 타입으로 반환
       // Enumeration형은 데이터를 뽑아올때 유용한 인터페이스 Enumeration객체는 java.util 패키지에 정의 되어 있으므로
       //java.util.Enumeration을 import 시켜야 한다.
       Enumeration files = multi.getFileNames();

       //업로드한 파일들의 이름을 얻어옴
       String file = (String) files.nextElement();
       filename = multi.getFilesystemName(file);
   }catch(Exception e) {
       //예외처리
       e.printStackTrace();
   }
   out.println("작성자 : "+name+"<br>>");
   out.println("제목 : "+subject+"<br>>");
%>
<html>
 <body>
  업로드 된 파일명 : <%=filename%><br>
 </body>
</html>




4) C:\web_upload_test\WEB-INF\ 폴더에서 web.xml 파일생성


[web.xml]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

  <display-name>web_upload_test</display-name>
  <description>web_upload_test</description>


  <session-config>
    <session-timeout>120</session-timeout>
  </session-config>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

<!--
  <taglib>
    <taglib-uri>/HJPTags</taglib-uri>
    <taglib-location>/WEB-INF/tlds/HJPTags.tld</taglib-location>
  </taglib>
-->

</web-app>


5) C:\web_upload_test\WEB-INF\lib\ 폴더에 cos.jar 파일 넣어두기



[실행 및 결과 캡쳐]

실행 :  C:\tomcat-6.0.10_web_upload_test\bin\startup.bat 실행





fileSelect.jsp 접속


간단히 입력하고 파일명 찾은후 파일올리기 버튼 누르면



파일 업로드완료 되면 fileUpload.jsp 결과화면이 표시됨.




업로드가 완료되면 c:\ 폴더에서 '월의_주차구하기.xlsm' 파일이 발견됨.































: