Springboot-일반적인 질문[jar 패키지 로 실 행 될 때 파일 을 업로드 할 수 없습니다!]
springboot 에서 개발 한 웹 프로젝트 는 Idea 에서 모든 것 을 정상적으로 실행 하지만 jar 로 포장 하여 로 컬 에 배치 한 후 다른 접근 은 정상 이지 만 파일 을 업로드 할 수 없습니다!
원인 분석:
jvm 에서 jar 를 실행 할 때 jar 패키지 에 대한 압축 을 풀 지 않 습 니 다.
해결 방법:
step 1:시작 클래스 에 경로 맵 설정 추가
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication .class, args);
}
//
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/","file:static/");
}
}
step 2:프로젝트 프로필 에 관련 설정 추가(properties 설정 을 예 로 들 면)
################ , resourceX fileDir
resourceX.fileDir = C:/Users/Administrator/Desktop/photo_album_jar/static
################
spring.resources.static-locations = classpath:static/,file:${resourceX.fileDir}
step 3:실행 할 jar 내 static 디 렉 터 리 를 jar 패키지 와 같은 등급 디 렉 터 리 로 이동 합 니 다.
step 4:다음 사용자 정의 호출 을 사용 하여 파일 저장 경 로 를 가 져 옵 니 다.
...
import org.springframework.util.ClassUtils;
import java.util.*;
...
private String imagePath;
// (config , “ ” )
private String getImagePath() {
if (null == imagePath) {
URL url = ClassUtils.getDefaultClassLoader().getResource("static");
if (url != null) {
// idea ,
String projectPath = url.getPath();
imagePath = projectPath + File.separator + config.imagePath.replace(".", File.separator) + File.separator;
} else {
// , jar “static ” jar
imagePath = "static"+File.separator+config.imagePath.replace(".", File.separator) + File.separator;
}
imagePath = FileDataSaveModule.adjustPathNameSeparator(imagePath);
logger.info(" :" + imagePath);
}
return imagePath;
}
...
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.