inblog logo
|
Uni
    JSPJAVA

    JSP 상품, 파일 업로드 연습 코드

    JSP, JAVA
    홍윤's avatar
    홍윤
    Jun 19, 2023
    JSP 상품, 파일 업로드 연습 코드
    파일업로드
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>20230619</title>
    </head>
    <body>
    <h3>업로드 할 파일을 선택하세여</h3>
    <form action="fileUpload_do.jsp" method="post" enctype="multipart/form-data">
    <input type="file" name="upload"> <br>
    <input type="submit" value="업로드">
    </form>
    </body>
    </html>
     
    Upload_do.jsp
    <%@page import="java.io.File"%>
    <%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
    <%@page import="com.oreilly.servlet.MultipartRequest"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>20230619</title>
    </head>
    <body>
    <%
    MultipartRequest multi = new MultipartRequest(request, //POST 방식으로 전송된 데이터를 담는 객체
    application.getRealPath("/files"), //파일 저장 경로
    100 * 1024 * 1024, // 100MB
    "UTF-8", new DefaultFileRenamePolicy()// 동일 파일명 처리 방법
    );
    File file = multi.getFile("upload"); //파일객체 얻기 <input type="file" name="upload"> input타입 file name을 설정 한 것을 넣기!
    if (file == null) {
    out.print("파일업로드 오류...");
    } else {
    out.print("File Name=" + file.getName() + "<br>");
    out.print("File Size=" + file.length());
    }
    %>
    </body>
    </html>
     
    상품페이지
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>20230619</title>
    </head>
    <body>
    <h1>상품등록</h1>
    <form action="fileUpload_do1.jsp" method="post" enctype="multipart/form-data">
    상품명: <input type="text" name="sname"> <br>
    상품사진: <input type="file" name="images">
    <input type="submit" value="전송">
    </form>
    </body>
    </html>
     
    fileUpload do1
     
    <%@page import="java.io.File"%>
    <%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
    <%@page import="com.oreilly.servlet.MultipartRequest"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>20230619</title>
    </head>
    <body>
    <%
    MultipartRequest multi = new MultipartRequest(request, //POST 방식으로 전송된 데이터를 담는 객체
    application.getRealPath("/files"), //파일 저장 경로
    100 * 1024 * 1024, // 100MB
    "UTF-8", new DefaultFileRenamePolicy()// 동일 파일명 처리 방법
    );
    String sname= multi.getParameter("sname");
    out.print("sname="+ sname+"<br>");
    File file = multi.getFile("images"); //파일객체 얻기 <input type="file" name="upload"> input타입 file name을 설정 한 것을 넣기!
    if (file == null) {
    out.print("파일업로드 오류...");
    } else {
    out.print("File Name=" + file.getName() + "<br>");
    out.print("File Size=" + file.length());
    }
    %>
    </body>
    </html>
    Servlet사용해서 만들기
    package Servlet;
    import java.io.File;
    import java.io.IOException;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import com.oreilly.servlet.MultipartRequest;
    import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
    import Bean.BookBean;
    /**
    * Servlet implementation class BookregServlet
    */
    @WebServlet("/BookregServlet")
    public class BookregServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public BookregServlet() {
    super();
    // TODO Auto-generated constructor stub
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    ServletContext ctx = request.getServletContext(); // 먼저 얻어 내야한다.
    MultipartRequest multi = new MultipartRequest(request, // POST 방식으로 전송된 데이터를 담는 객체
    ctx.getRealPath("/files"), // 파일 저장 경로
    100 * 1024 * 1024, // 100MB
    "UTF-8", new DefaultFileRenamePolicy()// 동일 파일명 처리 방법
    );
    File file = multi.getFile("filename");
    String title = multi.getParameter("title");
    String author = multi.getParameter("author");
    String filename = file.getName();
    BookBean book = new BookBean();
    book.setTitle(title);
    book.setAuthor(author);
    book.setFilename(filename);
    request.setAttribute("book", book);
    request.getRequestDispatcher("result.jsp").forward(request, response);
    // ctx.setAttribute("test", "test_applicateion");
    // response.sendRedirect("result.jsp");
    }
    }
     
    BookBean(VO)
    package Bean;
    import lombok.Data; import lombok.Getter; import lombok.Setter;
    @Setter @Getter @Data public class BookBean { private String title; private String author; private String filename;
    public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; }
    }
     
    regProd(form)
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>20230619 책 정보 등록</title>
    </head>
    <body>
    <h1>책 정보 등록</h1>
    <hr>
    <form action="BookregServlet" method="post" enctype="multipart/form-data">
    <input type="text" name="title"><br>
    <input type="text" name="author"> <br>
    <input type="file" name="filename"> <br>
    <input type="submit" value="등록">
    </form>
    <script>
    </script>
    </body>
    </html>
     
    result.jsp
    <%@ 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>
    <%--
    String test = (String)application.getAttribute("test");
    <!--
    application data(표현식) : <%= test %> <br>
    //application data(EL) : ${applicationScope.test}<br>
    -->
    --%>
    <table border="1">
    <thead>
    <tr>
    <th>제목</th>
    <th>저자</th>
    <th>이미지</th>
    </tr>
    </thead>
    <tr>
    <td>${book.title }</td>
    <td>${book.author }</td>
    <td><img src="files/${book.filename}" alt="표지이미지" width=100px></td>
    </tr>
    </table>
    </body>
    </html>
    Share article

    Uni

    RSS·Powered by Inblog