728x90
파일 업로드 / 다운로드
1. pom.xml에 commons-fileupload 추가
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
2. servlet-context에 multipartResolver 추가
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="defaultEncoding" value="UTF-8"></beans:property>
<beans:property name="maxUploadSize" value="-1"></beans:property>
</beans:bean>
defaultEncoding = 인코딩 방식
maxUploadSize= 최대 업로드 크기, -1인 경우 무한
3. form 수정
<form method="POST" enctype="multipart/form-data">
...
<input type="file" id="file" name="file" class="input-image" accept="image/*">
method: POST,
enctype=”multipart/form-data”로 설정
이미지 파일만 받고싶은 경우 accept="image/*"
4. ProductController
@PostMapping("/product/regist")
public String regist(Model model, @RequestPart(required = false) MultipartFile file, Product product)
throws IllegalStateException, IOException {
// 파일이 존재하는지 검사
if (file != null && file.getSize() > 0) {
UUID uuid = UUID.randomUUID();
String realName = file.getOriginalFilename(); // 파일 확장자를 포함한 전체 파일 이름
String saveFolder = "C:\\temp"; // 저장할 폴더 이름
String uniqueName = uuid.toString() + "_" + realName; // 중복 방지를 위한 uuid + 원래 파일 이름
product.setFileName(uniqueName);
product.setFileUri(saveFolder + "\\" + uniqueName);
file.transferTo(new File(saveFolder, uniqueName)); // saveFolder에 파일 업로드
}
}
@RequestPart(required = false)
→ 해당 필드가 쿼리스트링에 존재하지 않아도 예외가 발생하지 않음
https://velog.io/@lecharl/스프링12ResponseBody-파일3-다운로드
5. file download
@GetMapping("/download")
@ResponseBody
public void downloadFile(@RequestParam String fileName, HttpServletResponse response) throws IOException {
Map<String, Object> fileInfo = new HashMap<String, Object>();
fileInfo.put("filename", fileName);
response.setHeader("fileName", fileName.substring(fileName.lastIndexOf('_') + 1));
File file = new File("C:\\temp\\" + fileName); // 파일이 저장된 경로
byte[] data = FileUtils.readFileToByteArray(file); // 파일을 바이트로 변환
response.getOutputStream().write(data); // data를 클라이언트에 보냄
}
@ResponseBody
: 페이지 이동 없이 화면에 보여주는 것
다운로드 링크를 누르면 이미지가 뜨고 헤더에 fileName: temp.jfif 로 설정되어 있음
728x90
'BackEnd > Spring' 카테고리의 다른 글
[Spring] 로깅 logging (0) | 2022.12.12 |
---|---|
[Spring] 스프링 MyBatis 연동 (0) | 2022.11.01 |
[Spring] 스프링 인터셉터 - Handler Interceptor (0) | 2022.11.01 |
[Spring] 스프링 핵심 원리 기본편 9. 빈 스코프 (0) | 2022.10.28 |
[Spring] 스프링 핵심 원리 기본편 - 8. 빈 생명주기 콜백 (1) | 2022.10.27 |