반응형
250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 고차함수
- 자바스크립트객체리터럴
- 비주얼스튜디오
- EntityFramework
- 틱택토구현
- 인터넷프로토콜
- 자바스크립트recude
- 객체의비교
- 코딩
- 객체리터럴
- 이벤트리스너
- .NET
- 인프런강의
- 인프런자바스크립트
- sort
- 인프런무료강좌
- 자바스크립트파라미터
- NPM
- c#
- 인프런
- 자바스크립트
- slice
- 콜백함수
- 제로초
- Blazor
- HTTP
- 인프런인강
- 인프런강좌
- 자바스크립트틱택토
- 자바스크립트함수
Archives
- Today
- Total
샐님은 개발중
서블릿과 파일 업로드 본문
728x90
반응형
1. 파일 업로드 기본 예제 및 원리
package hello.upload.controller;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.util.Collection;
@Slf4j
@Controller
@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";
}
}
제출 버튼을 클릭하면 파일 업로드 컨트롤러의 saveFileV1 가 호출되면서 HTTP 요청 내용을 로그로 볼수 있다.
* 로그로 확인 하려면
application.properties 에 추가
logging.level.org.apache.coyote.http11=debug
saveFileV1의 로그 내용을 보면
상품 명과 첨부파일을 보낸 파트가 나눠서 보내지고 있다.
parts 에 2개가 있는 것을 확인해 볼 수 있다.
2. 실제 파일 서버 업로드 예제
1. application.properties 에 파일이 저장될 경로 추가
file.dir= C:\Users\ParkWonkyoung\Desktop\springmvc\file\
2. 파일 업로드 컨트롤러 작성
package hello.upload.controller;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
@Slf4j
@Controller
@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("=== PART ===");
log.info("name={}",part.getName());
Collection<String> headerNames = part.getHeaderNames();
for (String headerName : headerNames) {
log.info("header {}: {}", headerName,
part.getHeader(headerName));
}
//편의 메서드
//content-disposition; filename
log.info("submittedFileName={}", part.getSubmittedFileName());
log.info("size={}", part.getSize()); //part body size
//데이터 읽기
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";
}
}
3. 결과
728x90
반응형
'스프링 MVC 2편 - 인프런 김영한 > 섹션 11 - 파일 업로드' 카테고리의 다른 글
스프링과 파일 업로드 (0) | 2023.07.18 |
---|---|
파일 업로드 소개 및 프로젝트 생성 (0) | 2023.07.18 |