rhanziy

스프링 파일 업로드 관련 본문

Java

스프링 파일 업로드 관련

rhanziy 2022. 9. 5. 00:21

Model은 수집된 데이터 외에 보여주고 싶은 데이터를 view로 전달

@ModelAttribute("")

 

파일 업로드 

STEP1. commons-fileupload 라이브러리 추가

<dependency>

   <groupId>commons-fileupload</groupId> 

   <artifactId>commons-fileupload</artifactId>

   <version>1.3.3</version>

</dependency>

 

STEP2. C드라이브에 upload/tmp(임시업로드폴더) 생성

STEP3. servlet-context.xml에 CommonsMultipartResolver클래스의 빈과 빈 속성을 등록

 

   <beans:bean id="multipartResolver"      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

      <beans:property name="defaultEncoding" value="utf-8"></beans:property>

      <!-- 1024 * 1024 * 10 bytes 10MB -->

      <beans:property name="maxUploadSize" value="104857560">

      </beans:property>

      <!-- 1024 * 1024 * 2 bytes 2MB -->

      <beans:property name="maxUploadSizePerFile" value="2097152">

      </beans:property>

      <beans:property name="uploadTempDir" value="file:/C:/upload/tmp">

      </beans:property>

      <beans:property name="maxInMemorySize" value="10485756">

      </beans:property>

   </beans:bean>

 

절대경로로 지정할때는 file:/를 붙인당

 

STEP4. Controller에서 파일 업로드 화면 처리



@GetMapping("/upload")

public String upload() {

log.info("File Upload.......................");

 

return "upload";

}

 

@PostMapping("/exUpload")

public void exUpload(ArrayList<MultipartFile> files) {

files.forEach(file -> { 

log.info("exUpload..................................................");

log.info("name: "+file.getOriginalFilename());

log.info("name: "+file.getSize());

});

}

 

-----------------------------------------------------------------------------------upload.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>upload</title>

</head>

<body>

<form action="/sample/exUpload"  method="post" enctype="multipart/form-data">

<ul>

<li><input type="file" name="files"></li>

<li><input type="file" name="files"></li>

<li><input type="file" name="files"></li>

<li><input type="file" name="files"></li>

<li><input type="file" name="files"></li>

<li><input type="file" name="files"></li>

<li><input type="submit" value="SUBMIT"></li>

</ul>

 

<input type="file" name="files">

</form>

</body>

</html>

 

'Java' 카테고리의 다른 글

23.04.03. 작은 실습  (0) 2023.04.03
JSTL 개념, 문법, 셋팅  (0) 2023.03.30
스프링 주요 특징  (0) 2022.09.05
스프링 pom.xml, dbcp 설정 정리  (0) 2022.09.05
Java언어 특징 정리  (0) 2022.01.19
Comments