티스토리 뷰
package hello.upload.controller;
import hello.upload.domain.Item;
import hello.upload.domain.ItemForm;
import hello.upload.domain.ItemRepository;
import hello.upload.domain.UploadFile;
import hello.upload.file.FileStore;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.UriUtils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Slf4j
@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 itemForm, RedirectAttributes redirectAttributes) throws IOException {
UploadFile attachFile = fileStore.storeFile(itemForm.getAttachFile());
List<UploadFile> storeImageFiles = fileStore.storeFiles(itemForm.getImageFiles());
Item item = new Item();
item.setItemName(itemForm.getItemName());
item.setAttachFile(attachFile);
item.setImageFiles(storeImageFiles);
itemRepository.save(item);
redirectAttributes.addAttribute("itemId", item.getId());
return "redirect:/items/{itemId}";
}
@GetMapping("/items/{itemId}")
public String items(@PathVariable(value = "itemId") Long itemId, Model model){
Item item = itemRepository.findById(itemId);
model.addAttribute("item",item);
return "item-view";
}
//실제 이미지 경로의 것을 가져온다.
@ResponseBody
@GetMapping("/images/{filename}")
public Resource downloadImage(@PathVariable(value = "filename") String filename) throws MalformedURLException {
return new UrlResource("file:" + fileStore.getFullPath(filename));
}
@GetMapping("/attach/{itemId}")
public ResponseEntity<Resource> downloadAttach(@PathVariable(value="itemId")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 + "\""; // 한글이 섞이면 인코딩되야만 파일을 찾음
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
.body(resource);
}
}
package hello.upload.controller;
import hello.upload.domain.Item;
import hello.upload.domain.ItemForm;
import hello.upload.domain.ItemRepository;
import hello.upload.domain.UploadFile;
import hello.upload.file.FileStore;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.UriUtils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Slf4j
@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 itemForm, RedirectAttributes redirectAttributes) throws IOException {
UploadFile attachFile = fileStore.storeFile(itemForm.getAttachFile());
List<UploadFile> storeImageFiles = fileStore.storeFiles(itemForm.getImageFiles());
Item item = new Item();
item.setItemName(itemForm.getItemName());
item.setAttachFile(attachFile);
item.setImageFiles(storeImageFiles);
itemRepository.save(item);
redirectAttributes.addAttribute("itemId", item.getId());
return "redirect:/items/{itemId}";
}
@GetMapping("/items/{itemId}")
public String items(@PathVariable(value = "itemId") Long itemId, Model model){
Item item = itemRepository.findById(itemId);
model.addAttribute("item",item);
return "item-view";
}
//실제 이미지 경로의 것을 가져온다.
@ResponseBody
@GetMapping("/images/{filename}")
public Resource downloadImage(@PathVariable(value = "filename") String filename) throws MalformedURLException {
return new UrlResource("file:" + fileStore.getFullPath(filename));
}
@GetMapping("/attach/{itemId}")
public ResponseEntity<Resource> downloadAttach(@PathVariable(value="itemId")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 + "\""; // 한글이 섞이면 인코딩되야만 파일을 찾음
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
.body(resource);
}
}
'dev > spring boot + intelliJ' 카테고리의 다른 글
SpringBoot + Maven + JSP + Mybatis + PostgreSql 셋팅 (0) | 2024.05.30 |
---|---|
spring boot + maven + jsp 설정 (0) | 2024.05.28 |
파일 업로드 (3) 스프링이 제공하는 파일업로드 (0) | 2023.12.29 |
파일 업로드(2) 파일 업로드 시키기 (0) | 2023.12.27 |
파일 업로드 소개 (0) | 2023.12.25 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Intercepter
- 백엔드 개발자 역량
- 항해플러스
- rejectValue
- thymleaf
- 스프링부트
- HTTP
- filter
- Java
- 향해플러스
- 인터셉터
- 리터럴
- SpringBoot
- 컨트
- jpa api
- 항해99
- 스프링공부
- JPA
- reject
- BindingResult
- 예외처리
- hypertexttransferprotocol
- 향해플러스백엔드
- 백엔드 개발자 공부
- exception
- react실행
- 로그인
- 향해99
- React
- ArgumentResolver
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함