Springboot-일반적인 질문[jar 패키지 로 실 행 될 때 파일 을 업로드 할 수 없습니다!]

2128 단어 Java프레임
질문 설명:
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;
    }

...

 
 
 
 

좋은 웹페이지 즐겨찾기