스프링 MVC 2 (5)

Seung·2023년 2월 21일
0

파일 업로드

HTML 폼 전송 방식

  • multipart/form-data

    form 태그에 별도의 'enctype="multipart/form-data"를 지정해야한다.

서블릿을 이용한 파일 업로드

파일 업로드 경로설정
application.properties
file.dir=파일업로드 경로 (/Users/Seung/uplad/
마지막에 '/' 추가된 것에 주의

업로드 사이즈 제한
spring.servlet.multipart.max-file-size=1MB
spring.servlet.multipart.max-request-size=10MB
file-size: 파일하나의 최대사이즈, 기본 1MB
request-size: 여러파일 업로드시 전체합, 기본 10MB

public class ServletUploadController {

	//파일 업로드 경로설정 가져오기
	@Value("${file.dir}")
    private String fileDir;
    
	@PostMapping("/upload")
  	public String saveFile(HttpServletRequest request) throws ServletException, IOException{
        Collection<Part> parts = request.getParts();
        
        for (Part part : parts){
          	if(StringUtils.hasText(part.getSubmittedFileName())){
            	String fullPath = fileDir + part.getSubmittedFileName();
              	part.write(fullPath);
          	}
       } 
       return "upload-form";
   }
}

스프링 파일업로드

public class SpringUploadController {
	@Value("${file.dir}")
    private String fileDir;
    
    @PostMapping("upload")
    public String saveFile(@RequestParam MultipartFile file, HttpServletRequest request) throws IOException {
    	if (!file.isEmpty())
        {
        	String fullPath = fileDir + file.getOriginalFileName();
            file.transferTo(new File(fullPath));
        }
        return "upload-form";
    }
}

파일 업로드 예시

업로드파일 정보 보관
@Data
public class UploadFile {
	//업로드한 파일명
	private String uploadFileName;
    //서버관리 파일명
	private String storeFileName;
    
    public UploadFile(String uploadFileName, String storeFileName){
    	this.uploadFileName = uploadFileName;
        this.storeFileName = storeFileName;
    }
}
파일저장과 업무처리
@Component
public class FileStore {
	@Value("${file.dir}")
    private String fileDir;
	//파일 업로드 경로 + 파일명
    public String getFullPath(String filename) {
        return fileDir + filename;
    }
	//다중 파일 업로드
    public List<UploadFile> storeFiles(List<MultipartFile> multipartFiles) throws IOException {
        List<UploadFile> storeFileResult = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            if (!multipartFile.isEmpty()) {
                storeFileResult.add(storeFile(multipartFile));
            }
        }
        return storeFileResult;
    }


	//단일 파일업로드
    public UploadFile storeFile(MultipartFile multipartFile) throws IOException
    {
        if (multipartFile.isEmpty()) {
            return null;
        }
        String originalFilename = multipartFile.getOriginalFilename();
        String storeFileName = createStoreFileName(originalFilename);
        multipartFile.transferTo(new File(getFullPath(storeFileName)));
        return new UploadFile(originalFilename, storeFileName);
    }
    
	//서버관리 파일명 작성
    private String createStoreFileName(String originalFilename) {
        String ext = extractExt(originalFilename);
        String uuid = UUID.randomUUID().toString();
        return uuid + "." + ext;
    }
	//업로드파일 확장자 분리
    private String extractExt(String originalFilename) {
        int pos = originalFilename.lastIndexOf(".");
        return originalFilename.substring(pos + 1);
    }


}
상품 저장용 폼
@Data
public class ItemForm {

    private Long itemId;
    private String itemName;
    private MultipartFile attachFile;
    //다중업로드
    private List<MultipartFile> imageFiles;
}
컨트롤러
@Controller
@RequiredArgsConstructor
public class ItemController {

    private final ItemRepository itemRepository;
    private final FileStore fileStore;

    @GetMapping("/items/new")
    public String newItem(@ModelAttribute ItemForm form) {
        return "item-form";
    }

    @PostMapping("/items/new")
    public String saveItem(@ModelAttribute ItemForm form, RedirectAttributes redirectAttributes) throws IOException {
        UploadFile attachFile = fileStore.storeFile(form.getAttachFile());
        List<UploadFile> storeImageFiles = fileStore.storeFiles(form.getImageFiles());

        //데이터베이스에 저장
        Item item = new Item();
        item.setItemName(form.getItemName());
        item.setAttachFile(attachFile);
        item.setImageFiles(storeImageFiles);
        itemRepository.save(item);

        redirectAttributes.addAttribute("itemId", item.getId());
        return "redirect:/items/{itemId}";
    }

    @GetMapping("/items/{id}")
    public String items(@PathVariable Long id, Model model) {
        Item item = itemRepository.findById(id);
        model.addAttribute("item", item);
        return "item-view";

    }
	//이미지 파일 출력
    @ResponseBody
    @GetMapping("/images/{filename}")
    public Resource downloadImage(@PathVariable String filename) throws MalformedURLException {
        return new UrlResource("file:" + fileStore.getFullPath(filename));
    }
	//파일다운로드
    @GetMapping("/attach/{itemId}")
    public ResponseEntity<Resource> downloadAttach(@PathVariable Long itemId) throws MalformedURLException {
        Item item = itemRepository.findById(itemId);
        String storeFileName = item.getAttachFile().getStoreFileName();
        String uploadFileName = item.getAttachFile().getUploadFileName();

        UrlResource resource = new UrlResource("file:" + fileStore.getFullPath(storeFileName));
        log.info("uploadFileName={}", uploadFileName);

        String encodedUploadFileName = UriUtils.encode(uploadFileName, StandardCharsets.UTF_8);
        String contentDisposition = "attachment; filename=\"" + encodedUploadFileName + "\"";
        //String contentDisposition = "attachment; filename=\"" + uploadFileName + "\"";
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
                .body(resource);
    }
}
등록폼
<form th:action method="post" enctype="multipart/form-data">
 	<ul>
		<li>상품명 <input type="text" name="itemName"></li>
		<li>첨부파일<input type="file" name="attachFile" ></li>
		<li>이미지 파일들<input type="file" multiple="multiple" name="imageFiles" ></li>
	</ul>
	<input type="submit"/>
</form>
조회
상품명: <span th:text="${item.itemName}">상품명</span><br/>
첨부파일: <a th:if="${item.attachFile}" th:href="|/attach/${item.id}|" th:text="${item.getAttachFile().getUploadFileName()}" /><br/>
    <img th:each="imageFile : ${item.imageFiles}" th:src="|/images/${imageFile.getStoreFileName()}|" width="300" height="300"/>
profile
한번 해봅시다.

0개의 댓글