π© μ€νλ§μμ νμΌ μ λ‘λ ꡬν
μ€νλ§μμ νμΌ μ λ‘λλ₯Ό ꡬννλ €λ©΄ λͺ κ°μ§ μ€μ μ ν΄μ€μΌνλ€.
commons-fileupload λ₯Ό λ©μ΄λΈ λΌμ΄λΈλ¬λ¦¬ μμ‘΄μ± μ€μ μ μΆκ°νκ³ μΉ μ»΄ν¬λνΈ μ€μ νμΌ(servlet-context.xml)μ MultipartResolver λΉ μ€μ μ μΆκ°ν΄μΌνλ€.
1. λ©μ΄λΈ λΌμ΄λΈλ¬λ¦¬ μμ‘΄μ± μ€μ
<!-- File Upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
λ²μ μ μμ μ νλ‘μ νΈμ λ§κ² μ€μ ν΄μ£Όλ©΄ λλ€.
2. μΉ μ»΄ν¬λνΈ μ€μ νμΌ
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="50000000"/>
</beans:bean>
MultipartResolver λΉμ μμ±ν΄μ£Όλλ‘ νλ€. νμΌμ ν¬κΈ°λ λ무 ν¬λ©΄ μλκΈ° λλ¬Έμ μ΅λκ°μ μ§μ ν΄μ€λ€.
λλ bean idλ₯Ό μ€μ ν΄μ£Όμ§ μμμ form μμ λ°μ΄ν°κ° λμ΄κ°μ§ μμμ 컨νΈλ‘€λ¬μμ κ°μ λ°μ§ λͺ»νλ μ€λ₯κ° λ°μνμλ€.
κΌ xml λΉ μ€μ μ idλ₯Ό μ§μ ν΄μ£Όλλ‘ ν΄μΌνλ€.
π JSPμμ Form νκ·Έ μ΄μ©ν΄μ Controllerμ μ λ¬
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:url var="actionURL" value="/upload/new"/>
<form action="${actionURL}" method="post" enctype="multipart/form-data">
<select name="dir">
<option value="/">/
<option value="/images">/μ΄λ―Έμ§
</select>
<input type="file" name="file">
<input type="submit" value="SAVE">
<input type="reset" value="CANCEL">
</form>
</body>
</html>
μ λ‘λ νκΈ° μν κ°λ¨ν νΌμ΄λ€. μ΄μ κ°μ΄ Viewμμ form νκ·Έ μμ enctype="multipart/form-data" μμ±μ μΆκ°ν΄μ€λ€.
π Controller
@RequestMapping(value="/upload/new", method=RequestMethod.GET)
public String uploadFile(Model model) {
return "upload/form";
}
@RequestMapping(value="/upload/new", method=RequestMethod.POST)
public String uploadFile(@RequestParam(value="dir", required=false, defaultValue="/") String dir, @RequestParam MultipartFile file, RedirectAttributes redirectAttrs) {
logger.info(file.getOriginalFilename());
try {
if(file!=null && !file.isEmpty()) {
UploadFileVO newFile = new UploadFileVO();
newFile.setDirectoryName(dir);
newFile.setFileName(file.getOriginalFilename());
newFile.setFileSize(file.getSize());
newFile.setFileContentType(file.getContentType());
newFile.setFileData(file.getBytes());
uploadService.uploadFile(newFile);
}
}catch (Exception e) {
logger.error(e.getMessage());
redirectAttrs.addFlashAttribute("message", e.getMessage());
}
return "redirect:/upload/list";
}
컨νΈλ‘€λ¬μμ /upload/new λ‘ GET request μμ²μ΄ λ€μ΄μ€λ©΄ 보μ¬μ€ λ΄μ©λ€μ /upload/formμ λΏλ €μ€λ€.
κ·Έλ¦¬κ³ μ λ‘λ λ²νΌμ λλ₯΄κ² λλ©΄ formμ methodκ° Postμ΄κΈ° λλ¬Έμ post μμ²μ΄ λ€μ΄μ€κ² λλ€.
λ°λΌμ @RequestMapping(POST~)λ₯Ό DispatcherServletμ΄ μ°Ύμμ 맀νν΄μ€λ€. κ·Έ ν μμ μ΄ νμν μ 보λ€μ λ°μ΄ν°λ² μ΄μ€μμ κ°μ Έμ€λ©΄ λλ€.
κ·Έλ¦¬κ³ μ λ‘λκ° μλ£λλ©΄ μ¬μ§ λͺ©λ‘μ΄ μλ κ³³μΌλ‘ redirect ν΄μ£Όλ λ‘μ§μ μμ±νμλ€.
Serviceμ Repositoryλ Githubμ μ¬λ €λμκΈ° λλ¬Έμ λ€μμ κΈ°νκ° λλ€λ©΄ λ‘μ§μ μ€λͺ ν κ²μ΄λ€.
π» Github μ 체 μ½λ
https://github.com/wnstjd9701/spring-practice
'π» BackEnd > π’ Spring | SpringBoot' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
π’ [Spring] μ€νλ§ μμ‘΄μ± μ£Όμ (DI) - [2] (0) | 2023.07.07 |
---|---|
π’ [Spring] μ€νλ§ μμ‘΄μ± μ£Όμ (DI) - [1] (0) | 2023.07.06 |
π’ [Spring] μ€νλ§ μ€μ νμΌ (XML) (0) | 2023.06.28 |
π’ [Spring] μ€νλ§ λΉ(Bean) μ΄λ? (0) | 2023.06.23 |
π’ Spring Boot μμνκΈ° (0) | 2023.03.09 |