Skip to content

Commit

Permalink
파일 업로드 - 첨부파일의 다운로드 attachment 문구및 인코딩 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbell committed Aug 27, 2023
1 parent eda2d79 commit 8b31b8e
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
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.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
Expand Down Expand Up @@ -59,7 +63,24 @@ public Resource downloadImage(@PathVariable String filename) throws MalformedURL
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 urlResource = 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(urlResource);
}



Expand Down

0 comments on commit 8b31b8e

Please sign in to comment.