developer tip

Spring Boot를 사용하여 Dropbox 폴더에있는 정적 콘텐츠를 제공하려면 어떻게하나요?

copycodes 2020. 11. 30. 17:57
반응형

Spring Boot를 사용하여 Dropbox 폴더에있는 정적 콘텐츠를 제공하려면 어떻게하나요?


Spring Boot 웹 애플리케이션이 있고 Linode VPS (~ / Dropbox / images)의 공유 Dropbox 디렉터리에있는 정적 콘텐츠를 제공하고 싶습니다. Spring Boot가 자동으로 정적 콘텐츠를 제공한다는 것을 읽었습니다.

"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",

물론 내 Dropbox 디렉터리는 클래스 경로에 없습니다.

Dropbox 폴더의 이미지를 제공하도록 Apache를 구성 할 수 있지만 Spring Security를 ​​활용하여 인증 된 사용자에게 정적 콘텐츠 액세스를 제한하고 싶습니다.


고유 한 정적 리소스 핸들러를 추가 할 수 있습니다 (기본값 덮어 쓰기). 예 :

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:/path/to/my/dropbox/");
    }
}

이에 대한 문서가 Spring Boot 에 있지만 실제로는 단순한 Spring MVC 기능 일뿐입니다.

또한 스프링 부트 1.2 (내 생각에)부터 간단히 설정할 수 있습니다 spring.resources.staticLocations.


Springboot (Spring을 통해)는 이제 기존 리소스 핸들러에 쉽게 추가 할 수 있습니다. Dave Syers 답변을 참조하십시오 . 기존 정적 리소스 핸들러에 추가하려면 기존 경로를 재정의하지 않는 리소스 핸들러 경로를 사용하기 만하면됩니다.

아래의 두 "또한"메모는 여전히 유효합니다.

. . .

[편집 : 아래 접근 방식은 더 이상 유효하지 않습니다.]

기본 정적 리소스 핸들러 확장 하려면 다음과 같이 작동하는 것 같습니다.

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class CustomWebMvcAutoConfig extends
                    WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String myExternalFilePath = "file:///C:/Temp/whatever/m/";

    registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath);

    super.addResourceHandlers(registry);
  }

}

super.addResourceHandlers기본 핸들러 설정하는 호출 입니다.

또한:


@Dave Syers 답변을 기반으로 Spring Boot 프로젝트에 다음 클래스를 추가합니다.

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {

 private static final Logger LOG = LoggerFactory.getLogger(StaticResourceConfiguration.class);

 @Value("${static.path}")
 private String staticPath;

 @Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {

    if(staticPath != null) {
        LOG.info("Serving static content from " + staticPath);
        registry.addResourceHandler("/**").addResourceLocations("file:" + staticPath);
    }
 }

 // see https://stackoverflow.com/questions/27381781/java-spring-boot-how-to-map-my-my-app-root-to-index-html
 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("redirect:/index.html");
 }
}

This allows me to start my spring boot app with the parameter --static.path like

java -jar spring-app-1.0-SNAPSHOT.jar --static.path=/path/to/my/static-files/

This can be very handy for development and testing.


There's a property spring.resources.staticLocations that can be set in the application.properties. Note that this will override the default locations. See org.springframework.boot.autoconfigure.web.ResourceProperties.


@Mark Schäfer

Never too late, but add a slash (/) after static:

spring.resources.static-locations=file:/opt/x/y/z/static/

So http://<host>/index.html is now reachable.


Based on @Dave Syer, @kaliatech and @asmaier answers the springboot v2+ way would be:

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class StaticResourceConfiguration implements WebMvcConfigurer  {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String myExternalFilePath = "file:///C:/temp/whatever/m/";

     registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath);

  }

}

To serve from file system

I added spring.resources.static-location=file:../frontend/build in application.properties

index.html is present in the build folder

Use can also add absolute path

spring.resources.static-location=file:/User/XYZ/Desktop/frontend/build

I think similarly you can try adding Dropbox folder path.


For the current Spring-Boot Version 1.5.3 the parameter is

spring.resources.static-locations

Update I configured

`spring.resources.static-locations=file:/opt/x/y/z/static``

and expected to get my index.html living in this folder when calling

http://<host>/index.html

This did not work. I had to include the folder name in the URL:

http://<host>/static/index.html


FWIW, I didn't have any success with the spring.resources.static-locations recommended above; what worked for me was setting spring.thymeleaf.prefix:

report.location=file:/Users/bill/report/html/
spring.thymeleaf.prefix=${report.location}

Note that WebMvcConfigurerAdapter is deprecated now (see WebMvcConfigurerAdapter). Due to Java 8 default methods, you only have to implement WebMvcConfigurer.


  • OS: Win 10
  • Spring Boot: 2.1.2

I wanted to serve static content from c:/images

Adding this property worked for me:

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:///C:/images/

I found the original value of the property in the Spring Boot Doc Appendix A

This will make c:/images/image.jpg to be accessible as http://localhost:8080/image.jpg


You can place your folder in the root of the ServletContext.

Then specify a relative or absolute path to this directory in application.yml:

spring:
  resources:
    static-locations: file:some_temp_files/

The resources in this folder will be available (for downloading, for example) at:

http://<host>:<port>/<context>/your_file.csv

참고URL : https://stackoverflow.com/questions/21123437/how-do-i-use-spring-boot-to-serve-static-content-located-in-dropbox-folder

반응형