|
| 1 | +package hello.upload.controller; |
| 2 | + |
| 3 | +import hello.upload.domain.Item; |
| 4 | +import hello.upload.domain.ItemRepository; |
| 5 | +import hello.upload.domain.UploadFile; |
| 6 | +import hello.upload.file.FileStore; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.apache.coyote.Response; |
| 10 | +import org.springframework.core.io.Resource; |
| 11 | +import org.springframework.core.io.UrlResource; |
| 12 | +import org.springframework.http.HttpHeaders; |
| 13 | +import org.springframework.http.ResponseEntity; |
| 14 | +import org.springframework.stereotype.Controller; |
| 15 | +import org.springframework.ui.Model; |
| 16 | +import org.springframework.web.bind.annotation.*; |
| 17 | +import org.springframework.web.multipart.MultipartFile; |
| 18 | +import org.springframework.web.servlet.mvc.support.RedirectAttributes; |
| 19 | +import org.springframework.web.util.UriUtils; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.MalformedURLException; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +@Slf4j |
| 27 | +@Controller |
| 28 | +@RequiredArgsConstructor |
| 29 | +public class ItemController { |
| 30 | + |
| 31 | + private final ItemRepository itemRepository; |
| 32 | + private final FileStore fileStore; |
| 33 | + |
| 34 | + @GetMapping("/items/new") |
| 35 | + public String newForm(@ModelAttribute ItemForm form) { |
| 36 | + return "item-form"; |
| 37 | + } |
| 38 | + |
| 39 | + @PostMapping("/items/new") |
| 40 | + public String saveForm(@ModelAttribute ItemForm form, RedirectAttributes redirectAttributes) throws IOException { |
| 41 | + MultipartFile attachFile = form.getAttachFile(); |
| 42 | + UploadFile uploadFile = fileStore.storeFile(attachFile); |
| 43 | + |
| 44 | + List<MultipartFile> imageFiles = form.getImageFiles(); |
| 45 | + List<UploadFile> uploadFiles = fileStore.storeFiles(imageFiles); |
| 46 | + |
| 47 | + //데이터베이스에 저장 (보통 파일의 경로만 저장) (실제 파일은 s3 같은 곳에 저장) |
| 48 | + Item item = new Item(); |
| 49 | + item.setItemName(form.getItemName()); |
| 50 | + item.setAttachFile(uploadFile); |
| 51 | + item.setImageFiles(uploadFiles); |
| 52 | + itemRepository.save(item); |
| 53 | + |
| 54 | + redirectAttributes.addAttribute("itemId", item.getId()); |
| 55 | + |
| 56 | + return "redirect:/items/{itemId}"; |
| 57 | + } |
| 58 | + |
| 59 | + @GetMapping("/items/{id}") |
| 60 | + public String items(@PathVariable Long id, Model model) { |
| 61 | + Item item = itemRepository.findById(id); |
| 62 | + model.addAttribute("item", item); |
| 63 | + return "item-view"; |
| 64 | + } |
| 65 | + |
| 66 | + @ResponseBody |
| 67 | + @GetMapping("/images/{fileName}") |
| 68 | + public Resource downloadImage(@PathVariable String fileName) throws MalformedURLException { |
| 69 | + return new UrlResource("file:" + fileStore.getFullPath(fileName)); //해당 리소스에 있는 파일을 가져온다. |
| 70 | + } |
| 71 | + |
| 72 | + //첨부파일 다운로드 |
| 73 | + @GetMapping("/attach/{itemId}") |
| 74 | + public ResponseEntity<Resource> downloadAttach(@PathVariable Long itemId) throws MalformedURLException { |
| 75 | + Item item = itemRepository.findById(itemId); |
| 76 | + String storeFileName = item.getAttachFile().getStoreFileName(); |
| 77 | + String uploadFileName = item.getAttachFile().getUploadFileName(); |
| 78 | + |
| 79 | + UrlResource urlResource = new UrlResource("file:" + fileStore.getFullPath(storeFileName)); |
| 80 | + |
| 81 | + log.info("uploadFileName={}", uploadFileName); |
| 82 | + |
| 83 | + String encodedUploadFileName = UriUtils.encode(uploadFileName, StandardCharsets.UTF_8); //파일명 인코딩 => 한글 안깨짐 |
| 84 | + String contentDisposition = "attachment; filename=\"" + encodedUploadFileName + "\""; |
| 85 | + |
| 86 | + return ResponseEntity.ok() |
| 87 | + .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition) //body에 있는 파일을 다운로드 하라 (contentDisposition 이라는 이름으로) |
| 88 | + .body(urlResource); |
| 89 | + } |
| 90 | +} |
0 commit comments