[7장 연습문제]

 

01. 파일 업로드를 위한 form 태그내에 반드시 설정해야 하는 기법은 무엇인가?

 

1. action 속성-> 파일 업로드를 처리할 JSP파일 설정

2. method 속성-> 반드시 POST방식으로 설정

3. enctype 속성-> multipart/form-data로 설정

 

02. 파일을 서버에 업로드하는 처리 기법에 대해 간단히 설명하시오.

 

1. MultipartRequest를 이용한 파일 업로드

 MultipartRequest 객체 생성-> 해당 클래스가 제공하는 메소드를 사용하여 요청 파라미터를 읽어오고 파일 업로드

* 03번 문항 참조

 

2. Commons-FileUpload를 이용한 파일 업로드

 DiskFileUpload 객체 생성-> 해당 클래스가 제공하든 메소드를 사용하여 요청 파라미터를 읽어오고 

 FileItem 클래스의 메소드를 이용하여 요청 파라미터를 분석(일반데이터 or 파일)및 처리하여 파일 업로드

 

03. MultipartRequest 클래스를 이용하여 조건에 맞게 JSP애플리케이션을 만들고 실행결과를 확인하시오.

* 서버의 파일 저장 폴더 이름: C:\\exercise

   

 

<폴더>

 

 

1. WEB-INF/lib/폴더에 cos.jar파일 추가

2. fileupload01.jsp 파일 생성

3. fileupload01_process.jsp 파일 생성

 

<코드>

1. fileupload01_jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File Upload</title>
</head>
<body>
<form name="fileForm" method="post" enctype="multipart/form-data" action="fileupload01_process.jsp">
        <p> 파일: <input type="file" name="filename">
        <p> <input type="submit" value="파일 올리기">
    
    </form>
</body>
</html>
cs

 

 

2. fileupload01_process.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="com.oreilly.servlet.*"%>
<%@ page import="com.oreilly.servlet.multipart.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%
    MultipartRequest multi = new MultipartRequest(request, "C:\\exercise"5 * 1024 * 1024"utf-8"new DefaultFileRenamePolicy());
 
    Enumeration params = multi.getParameterNames();
 
    while (params.hasMoreElements()) {
        String name = (String) params.nextElement();
        String value = multi.getParameter(name);
        out.println(name + " = " + value + "<br>");
    }
    out.println("-----------------------------------<br>");
 
    Enumeration files = multi.getFileNames();
 
    while (files.hasMoreElements()) {
        String name = (String) files.nextElement();
        String filename = multi.getFilesystemName(name);
        String original = multi.getOriginalFileName(name);
        String type = multi.getContentType(name);
        File file = multi.getFile(name);
 
        out.println("요청 파라미터 이름 : " + name + "<br>");
        out.println("실제 파일 이름 : " + original + "<br>");
        out.println("저장 파일 이름 : " + filename + "<br>");
        out.println("파일 콘텐츠 유형 : " + type + "<br>");
 
        if (file != null) {
            out.println(" 파일 크기 : " + file.length());
            out.println("<br>");
        }
    }
%>
cs

 

 

04. Commons-FileUpload 패키지를 이용하여 조건에 맞게 JSP애플리케이션을 만들고 결과를 확인하시오.

    

 

<폴더>

 

 

1. WEB-INF/lib 폴더에 commons-fileupload.jar, commons-io.jar 파일 추가

2. fileupload02.jsp파일 생성

3. fileupload02_process.jsp파일 생성

 

<코드>

1. fileupload02.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form name="fileForm" method="post" enctype="multipart/form-data" action="fileupload02_process.jsp">
        <p> 파일: <input type="file" name="filename">
        <p> <input type="submit" value="파일 올리기">
    
    </form>
</body>
</html>
cs

 

2. fileupload02_process.jsp

* 16~19행: 파일의 최대 크기/저장 버퍼 크기/ 파일 저장경로 설정(c:\\exercise)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<%@ page  contentType="text/html; charset=UTF-8"%>
<%@page import="org.apache.commons.fileupload.*" %>
<%@page import="java.util.*" %>
<%@page import="java.io.*" %>
 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
            String Path="C:\\exercise";
    
    DiskFileUpload upload=new DiskFileUpload();
    upload.setSizeMax(1000000);
    upload.setSizeThreshold(4096);
    upload.setRepositoryPath(Path);
    
    List items=upload.parseRequest(request);
    Iterator params=items.iterator();
    while(params.hasNext())
    {
        FileItem item=(FileItem)params.next();
        if(!item.isFormField())
        {
            String fileFiledName=item.getFieldName();
            String fileName=item.getName();
            String contentType=item.getContentType();
            
            fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
            long fileSize=item.getSize();
            
            File file=new File(Path+"/"+fileName);
            item.write(file);
            
            out.println("요청 파라미터 이름:"+fileFiledName+"<br>");
            out.println("저장 파일 이름:" +fileName+"<br>");
            out.println("파일 콘텐츠 유형:"+contentType+"<br>");
            out.println("파일 크기: "+fileSize);
        }
    }
    %>
</body>
</html>
cs
 
 
 
 
 

 

+ Recent posts