upload : file upload(servlet)
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package hello.upload.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.Part;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
@RequestMapping("/servlet/v1")
|
||||
public class ServletUploadControllerV1 {
|
||||
|
||||
@GetMapping("/upload")
|
||||
public String newFile() {
|
||||
return "upload-form";
|
||||
}
|
||||
|
||||
@PostMapping("/upload")
|
||||
public String saveFileV1(HttpServletRequest request) throws ServletException, IOException {
|
||||
log.info("request={}", request);
|
||||
|
||||
String itemName = request.getParameter("itemName");
|
||||
log.info("itemName={}", itemName);
|
||||
|
||||
Collection<Part> parts = request.getParts();
|
||||
log.info("parts={}", parts);
|
||||
|
||||
return "upload-form";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package hello.upload.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.Part;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
@RequestMapping("/servlet/v2")
|
||||
public class ServletUploadControllerV2 {
|
||||
|
||||
@Value("${file.dir}")
|
||||
private String fileDir;
|
||||
|
||||
@GetMapping("/upload")
|
||||
public String newFile() {
|
||||
return "upload-form";
|
||||
}
|
||||
|
||||
@PostMapping("/upload")
|
||||
public String saveFileV1(HttpServletRequest request) throws ServletException, IOException {
|
||||
log.info("request={}", request);
|
||||
|
||||
String itemName = request.getParameter("itemName");
|
||||
log.info("itemName={}", itemName);
|
||||
|
||||
Collection<Part> parts = request.getParts();
|
||||
log.info("parts={}", parts);
|
||||
|
||||
for (Part part : parts) {
|
||||
log.info("===== Parts =====");
|
||||
log.info("name={}", part.getName());
|
||||
Collection<String> headerNames = part.getHeaderNames();
|
||||
for (String headerName : headerNames) {
|
||||
log.info("header {}: {}", headerName, part.getHeader(headerName));
|
||||
}
|
||||
|
||||
log.info("submittedFileName={}", part.getSubmittedFileName());
|
||||
log.info("size={}", part.getSize());
|
||||
|
||||
// 데이터 읽기
|
||||
InputStream inputStream = part.getInputStream();
|
||||
String body = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
|
||||
log.info("body={}", body);
|
||||
|
||||
// 저장하기
|
||||
if (StringUtils.hasText(part.getSubmittedFileName())) {
|
||||
String fullPath = fileDir + part.getSubmittedFileName();
|
||||
log.info("파일 저장 fullPath={}", fullPath);
|
||||
part.write(fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
return "upload-form";
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
#logging.level.org.apache.coyote.http11=debug
|
||||
|
||||
file.dir=C:/Users/Woojin/Desktop/study/file/
|
||||
21
upload/src/main/resources/templates/upload-form.html
Normal file
21
upload/src/main/resources/templates/upload-form.html
Normal file
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="py-5 text-center">
|
||||
<h2>상품 등록 폼</h2>
|
||||
</div>
|
||||
<h4 class="mb-3">상품 입력</h4>
|
||||
<form th:action method="post" enctype="multipart/form-data">
|
||||
<ul>
|
||||
<li>상품명 <input type="text" name="itemName"></li>
|
||||
<li>파일<input type="file" name="file" ></li>
|
||||
</ul>
|
||||
<input type="submit"/>
|
||||
</form>
|
||||
</div> <!-- /container -->
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user