on
1022_JSP/Servlet : 파일처리
1022_JSP/Servlet : 파일처리
ex01
<%@ page contentType="text/html; charset=UTF-8"%> <%@ page trimDirectiveWhitespaces="true"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> Insert title here 파일 업로드 <%-- - form 태그에서 enctype을 생략하면 기본이 application/x-www-form-urlencoded - application/x-www-form-urlencoded 속성 값은 파라미터를 주소형식으로 인코딩하여 전송 파라미터는 "이름1=값1&이름=값2" 형태로 body영역에 실어서 전송된다. - application/x-www-form-urlencoded 속성 값에서 파일을 서버로 전송하면 파일 이름만 전송 된다. - enctype 속성은 method가 post인 경우만 유효 --%> 제목 : 파일 : 등록하기
ex01_ok
<%@ page contentType="text/html; charset=UTF-8"%> <%@ page trimDirectiveWhitespaces="true"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <% request.setCharacterEncoding("utf-8"); // request.getParameter()는 문자열만 전달 받는다. String subject = request.getParameter("subject"); // 파일은 enctype="application/x-www-form-urlencoded" 에서는 이름만 전송 받는다. String selectFile = request.getParameter("selectFile"); %> Insert title here 요청 받은 정보 제목 : <%= subject %> 파일 : <%=selectFile %>
위 예제를 통해서 파일을 전달 받지 못하는 것을 알 수 있다. 파일의 이름만을 받았음.
ex02
<%@ page contentType="text/html; charset=UTF-8"%> <%@ page trimDirectiveWhitespaces="true"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> Insert title here 파일 업로드 <%-- * form 태그에서 enctype이 multipart/form-data인 경우 - 문자 인코딩을 하지 않고 전송하며, 파일을 전송할 때 사용 - 피일의 내용도 전송된다. * enctype 속성은 method가 post인 경우만 유효 --%> 제목 : 파일 : 등록하기
ex02_ok
<%@page import="java.net.URLDecoder"%> <%@page import="java.io.InputStream"%> <%@page import="java.util.Enumeration"%> <%@ page contentType="text/html; charset=UTF-8"%> <%@ page trimDirectiveWhitespaces="true"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <% request.setCharacterEncoding("utf-8"); // enctype="multipart/form-data"로 넘어온 파라미터는 request.getParameter()로 받을 수 없다. // String subject = request.getParameter("subject"); // String selectFile = request.getParameter("selectFile"); %> Insert title here 요청 받은 정보 <% String contentType = request.getContentType(); out.print("contentType : " + contentType + ""); out.print(""); out.print("[[헤더 정보들]]...."); Enumeration e = request.getHeaderNames(); while(e.hasMoreElements()) { String name = e.nextElement(); String value = request.getHeader(name); out.print("" + name + " : " + value + ""); } out.print(""); out.print("[[request Body 영역으로 넘어온 데이터]]"); InputStream is = request.getInputStream(); byte[] buf = new byte[2048]; int size; String str; while( (size = is.read(buf)) != -1) { // enctype = "application/x-www-form-urlencoded" 인 경우 // str = new String(buf, 0, size); // str = URLDecoder.decode(str, "utf-8"); // enctype = "nultipart/form-data" 인 경우 str = new String(buf, 0, size, "utf-8"); out.print("" + str + ""); } out.print(""); %>
파일이 넘어왔음을 알 수 있다.
cos.jar을 이용한 업로드 (권장하지 않음)
<%@ page contentType="text/html; charset=UTF-8"%> <%@ page trimDirectiveWhitespaces="true"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> Insert title here 파일 업로드 <%-- * cos.jar를 이용한 업로드 http://servlets.com --%> 제목 : 파일 : 등록하기
from http://development-writing.tistory.com/335 by ccl(S) rewrite - 2021-10-25 06:28:17