Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Tags more
Archives
Today
Total
관리 메뉴

기록

JSP 파일업로드 4 (숙제) 본문

JSP

JSP 파일업로드 4 (숙제)

9400 2023. 1. 4. 14:00

폼 데이터 만들어주고 

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<jsp:include page="/header.jsp"></jsp:include>


<div class="card card-primary">
<div class="card-header">
                <h3 class="card-title">Quick Example</h3>
              </div>



   <form name="frm" id="frm" action="fileupload05_process.jsp" method="post"
   enctype="multipart/form-data">
		<div class="card-body">
		<div class="form-group">
		<label for="memMail">Email address</label>
		<input type="email" class="form-control" id="memMail" name="memMail" placeholder="Enter email" />
		</div>
		<div class="form-group">
		<label for="password">Password</label>
		<input type="password" class="form-control" id="password" name="passwords" placeholder="Password">
		</div>
		<div class="form-group">
		<label for="exampleInputFile">File input</label>
		<div class="input-group">
		<div class="custom-file">
		<input type="file" class="custom-file-input" id="filename" name="filename">
		<label class="custom-file-label" for="filename">Choose file</label>
		</div>
		<div class="input-group-append">
		<span class="input-group-text">Upload</span>
		</div>
		</div>
		</div>
		<div class="form-check">
		<!-- 스프링 시큐리티의 로그인 유지기능 사용 remember-me -->
		<input type="checkbox" class="form-check-input" id="remember-me" name="remember-me">
		<label class="form-check-label" for="remember-me">Check me out</label>
		</div>
		</div>
		
		<div class="card-footer">
		<button type="submit" class="btn btn-primary">Submit</button>
		</div>
</form>
</div>
   

<jsp:include page="/bottom.jsp"></jsp:include>

 

처리페이지

 

commons-fileupload.jar 오픈 라이브러리 
jsp의 경우 DiskFileUpload 객체를 사용한다 

<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	request.setCharacterEncoding("utf-8");
	//파일저장 path 
	String path = "c:\\upload";
	
	DiskFileUpload upload = new DiskFileUpload();
	upload.setSizeMax(1000000);
	upload.setSizeThreshold(4096);
	upload.setRepositoryPath(path);
	
	//요청파라미터를 받음
	//formField(memMail=22@google.com&password=1234) 2개
	//파일객체(filename이라는 파일객체) 1개 
	
	//request객체를 가져와서 리스트에 담아줌
	List items = upload.parseRequest(request);
	
	//열거형으로 만듦
	Iterator params = items.iterator();
	
	//열거형을 하나씩 꺼내줌
	
	while(params.hasNext()){
		//FileItem : 1)일반데이터(memMail, password)
		//			 2)파일(filename)
		FileItem item = (FileItem)params.next();
		
		if(item.isFormField()){ //만약 파일아이템을 꺼낸게 일반데이터이면
			String name = item.getFieldName(); //파라미터의 name값을 가져옴(memMail,password)
			String value= item.getString(); //파라미터의 value값을 가져옴 (내가 입력한 값)
			
			out.print("<p>"+name+" : "+value+"</p>");
		}else{ //그게 아니면 , (파일데이터이면)
			String fileFieldName= item.getFieldName(); //파라미터의 name값을 꺼내주고
			String fileName = item.getName(); //경로가 포함된 전체를 가져옴
			String contentType = item.getContentType(); //Mine 타입(만약 이미지:images/jpeg)
			
			//파일명만 추출
			//c://Usuer\\test\\picutres\\개똥이.png
			//마지막 \\의 위치를 찾아서 +1 을 하면 "개" 부터 끝까지 추출 -> 개똥이.png
			
			fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
			long fileSize = item.getSize();
			
			//내가 저장할 경로설정 및 파일이름 설정
			File file = new File(path+"/"+fileName);
			
			//내가 가지고있는 파일을 file에 복사한다! 
			item.write(file);
			
			out.print("=============<br />");
			out.print("요청 파라미터 이름:"+fileFieldName+"<br />");
			out.print("저장 파일 이름:"+fileName+"<br />");
			out.print("파일콘텐츠유형"+contentType+"<br />");
			out.print("파일크기:"+fileSize+"<br />");
		}
	}
%>

처리 결과

c-upload폴더에도 사진이 잘 들어간것을 확인했다면 성공! 

'JSP' 카테고리의 다른 글

JSP 상품등록 + 파일업로드 추가5  (0) 2023.01.05
JSP 상품 등록 후 자동 새로고침 설정  (0) 2023.01.05
JSP 파일업로드 3  (0) 2023.01.04
JSP 파일업로드 2  (0) 2023.01.04
JSP 파일업로드  (0) 2023.01.04
Comments