강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

작성자 없음

작성자 정보가 삭제된 글입니다.

스프링 MVC 2편 - 백엔드 웹 개발 활용 기술

예제로 구현하는 파일 업로드, 다운로드

경로는 찾으나 에러가뜹니다

작성

·

405

0

이미지는 엑박으로 표기.
->새탭에서 이미지열기시, 나오는경로-
http://localhost:8080/images/C:/Users/계정/springfile/images/c3a2eedd-4364-4f3f-a275-a800512e4949.png

파일다운로드 클릭시 경로와 에러페이지,
http://localhost:8080/attach/C:/Users/계정/springfile/uploadfiles/9aa381e7-6b8d-412a-bf86-28caf5b29010.pdf

으로 됩니다.

----

save시 컴퓨터 경로에 사진, 파일이 저장이되고
db에도 잘 저장됩니다..

코드

<img th:each="imageFile : ${board.attachFiles}"
th:if="${imageFile.attachType.toString().equals('IMAGE')}"
th:src="|/images/${imageFile.storeFileName}|" width="300" height="300" style="margin-right: 5px"/>
<a th:each="uploadFile : ${board.attachFiles}"
th:if="${uploadFile.attachType.toString().equals('FILE')}"
th:href="@{/attach/{filename}(filename=${uploadFile.storeFileName}, originName=${uploadFile.uploadFileName})}"
th:text="${uploadFile.uploadFileName}" style="margin-right: 5px"/><br/>
@ResponseBody
@GetMapping("/images/{filename}")
public Resource processImg(@PathVariable String filename) throws MalformedURLException {
return new UrlResource("file:" + fileStore.getFullPath(filename, AttachType.IMAGE));

@GetMapping("/attach/{filename}")
public ResponseEntity<Resource> processAttaches(@PathVariable String filename, @RequestParam String originName) throws MalformedURLException {
UrlResource resource = new UrlResource("file:" + fileStore.getFullPath(filename, AttachType.FILE));

String encodedUploadFileName = UriUtils.encode(originName, StandardCharsets.UTF_8);
String contentDisposition = "attachment; filename=\"" + encodedUploadFileName + "\"";

return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
.body(resource);
}

 

 

 

 

 

 

 

 

답변 1

0

안녕하세요. 스프님, 공식 서포터즈 David입니다.

강의와 다르게 작성하신 것 같습니다.

강의 내용대로 작성하시고 난 뒤 동작유무를 확인해주세요.

감사합니다.

작성자 없음

작성자 정보가 삭제된 글입니다.

질문하기