IT/JSP

[쉽게 배우는 JSP 웹 프로그래밍 개정2판] 7장 설명, 연습 문제

곰탱이들 2024. 6. 5.

7장 연습문제 소스코드

7장 연습문제

 

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

  • 4번 파일 업로드를 위해 input태그의 type속성을 file로 설정해야 한다.만약 여러 파일을 업로드하 려면 2개 이상의 input 태그를 사용하고 name 속성에 서로 다른 같은 값으로 설정해야 한다.

2. MultipartRequest를 이용한 파일 업로드 메소드의 종류로 옳지 않은 것은 무엇인가?

  • 4번 getFileName(): 폼 페이지에 input 태그의 type 속성값이 file로 설정된 요청 파라미터의 이름을 반환한다.

3. MultipartRequest를 이용한 파일 업로드로, 메소드에서 사용자가 설정하여 서버에 실제로 업 로드된 파일명을 반환하고 파일명이 중복되면 변경된 파일명을 반환하는 메소드는 무엇인가?

  • 2번 getFilesystemName()

4. MultipartRequest를 이용한 파일 업로드로, 메소드에서 사용자가 업로드한 실제 파일명을 반환하고, 파일명이 중복되면 변경 전의 파일명을 반환하는 메소드는 무엇인가?

  • 3번 getOriginalFileName()

5. 다음은 MultipartRequest 클래스를 이용하여 파일 업로드 및 정보를 출력하는 프로 그램이다.밑줄에 들어갈 올바른 것은 무엇인가?

  • 1번 multipart/form-data, file

6. 다음은 MultipartRequest 클래스를 이용하여 파일을 업로드하는 프로그램이다.밑줄 에 들어갈 올바른 것은 무엇인가?

  • 4번 newDefaultFileRenamePolicy()

7. DiskFileUpload를 이용한 파일 업로드 메소드의 종류로 옳지 않은 것은 무엇인가?

  • 4번 setParseRequest() : multipart/form-data 유형의 요청 파라미터를 가져온다.

8. DiskFileUpload를 이용한 파일 업로드에서 FileItem클래스의 메소드 종류로 옳지 않은 것은 무엇인가?

  • 1번 setWrite() : 파일과 관련된 자원을 저장한다.

소스코드

9.

<!DOCTYPE html>
<html>
<head>
    <title>File Upload Example</title>
</head>
<body>
    <h2>File Upload Example</h2>
    <form action="fileupload01_process.jsp" method="post" enctype="multipart/form-data">
        파일:
        <input type="file" name="찾아보기..." id="fileUpload">
        <input type="submit" value="Upload File" name="파일 올리기">
    </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.Enumeration" %>
<!DOCTYPE html>
<html>
<head>
    <title>File Upload Processing</title>
</head>
<body>
    <h2>File Upload Processing</h2>
    <% 
        String uploadDir = getServletContext().getRealPath("/uploads");

        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdir();
        }

        int maxFileSize = 10 * 1024 * 1024;
        String encoding = "UTF-8";
        MultipartRequest multipartRequest = new MultipartRequest(request, uploadDir, maxFileSize, encoding);

        Enumeration files = multipartRequest.getFileNames();
        while (files.hasMoreElements()) {
            String fileName = (String) files.nextElement();
            String originalFileName = multipartRequest.getOriginalFileName(fileName);
            String contentType = multipartRequest.getContentType(fileName);
            File uploadedFile = multipartRequest.getFile(fileName);

            out.println("요청 파라미터 이름: " + fileName + "<br>");
            out.println("실제 파일 이름: " + originalFileName + "<br>");
            out.println("저장 파일 이름: " + uploadedFile.getAbsolutePath() + "<br>");
            out.println("파일 컨텐츠 유형: " + contentType + "<br>");
            out.println("파일 크기: " + uploadedFile.length() + " bytes<br>");
        }
    %>
</body>
</html>

 

10.

<!DOCTYPE html>
<html>
<head>
    <title>File Upload Example</title>
</head>
<body>
    <h2>File Upload Example</h2>
    <form action="fileupload02_process.jsp" method="post" enctype="multipart/form-data">
        파일 : 
        <input type="file" name="찾아보기" id="fileUpload">
        <input type="submit" value="Upload File" name="파일 올리기">
    </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<!DOCTYPE html>
<html>
<head>
    <title>File Upload Processing</title>
</head>
<body>
    <h2>File Upload Processing</h2>
    <% 
        String uploadDir = getServletContext().getRealPath("/uploads");

        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdir();
        }

        int maxFileSize = 10 * 1024 * 1024;
        String encoding = "UTF-8";

        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(maxFileSize);
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            List<FileItem> items = upload.parseRequest(request);

            for (FileItem item : items) {
                if (!item.isFormField()) {
                    String fileName = new File(item.getName()).getName();
                    String contentType = item.getContentType();
                    long fileSize = item.getSize();

                    out.println("요청 파라미터 이름: " + item.getFieldName() + "<br>");
                    out.println("저장 파일 이름: " + fileName + "<br>");
                    out.println("파일 컨텐츠 타입: " + contentType + "<br>");
                    out.println("파일 크기: " + fileSize + " bytes<br>");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    %>
</body>
</html>

댓글