파일 업로드를 할 때
저장하고 싶은 위치는
src\main\static\resources\upload 인데
자꾸
src\main\webapp\static\resources\upload
에 저장되는 문제가 있었다.
해결은 아직 하지 못했다..
http://localhost:8080/resources/upload/test.png
라고 주소창에 검색하면 static\resources\upload에 있는 파일이 잘 뜨는데
업로드할 때 주소 설정이 잘못된 것인지 파일 업로드만 하면 webapp으로 간다 뭔가 설정이 잘못된 것 같은데 아직 해결 방안을 찾지 못함
@PostMapping("/regist")
public String doRegist(@ModelAttribute User user, @RequestPart(required = false) MultipartFile file, Model model)
throws IllegalStateException, IOException {
// 먼저 파일이 존재하는지 검사
if (file != null && file.getSize() > 0) {
// 파일을 저장할 위치 지정
Resource res = resLoader.getResource("resources/upload");
// 중복방지를 위해 파일 이름앞에 현재 시간 추가
user.setImg(System.currentTimeMillis() + "_" + file.getOriginalFilename());
// User 객체에 원본 파일 이름 저장
user.setOrgImg(file.getOriginalFilename());
// 파일 저장
file.transferTo(new File(res.getFile().getCanonicalPath() + "/" + user.getImg()));
System.out.println(res.getFile().getCanonicalPath());
}
// DB에 user 정보 등록
service.insert(user);
return "/regist_result";
}
221102 해결 완!
https://www.folkstalk.com/2022/10/spring-boot-save-file-to-static-folder-with-code-examples.html
Spring Boot Save File To Static Folder With Code Examples
Spring Boot Save File To Static Folder With Code Examples Hello guys, in this post we will explore how to find the solution to Spring Boot Save File To Static Folder in programming. String fileLocation = new File("src\main\resources\static\uploads").getAbs
www.folkstalk.com
결국 절대 경로를 활용해야 했던 것 같다
아직 프로젝트에서 경로를 찾을 때 현재 위치와 classpath 등 경로를 이해하기가 힘든 것 같다
// 파일 저장
String fileLocation = new File("src\\main\\resources\\static\\resources\\upload").getAbsolutePath();
결국 context path부터 src/main/resources/static/resources/upload를 쭉 써주는 것으로 해결했다
static 경로를 간단히 지정해서 해결하는 방법이 있을 것 같은데 아직은 발견하지 못함..
아니면 static 경로에 있는 파일을 읽어오는 것은 resourceHandler로 가능한데, 업로드하는 것은 절대 경로를 활용해야 하는 것 같기도 하다!
'Trouble Shootings' 카테고리의 다른 글
[Vue.js] .env .local 값을 못 읽을 때 (403 error) (0) | 2022.11.20 |
---|---|
[intellij/spring] 인텔리제이 spring boot annotation "Cannot read symbol" (0) | 2022.11.20 |
[크롬 Chrome] 확장 프로그램 다운로드 / 실행 실패 (0) | 2022.11.07 |
[스프링부트] Web server failed to start. Port 8080 was already in use. (0) | 2022.10.27 |
[자바] java.util.ConcurrentModificationException 에러 (0) | 2022.10.26 |