简体   繁体   English

将pdf作为在docker容器中运行的spring应用程序的静态资源提供

[英]Serve pdf as static resource from spring application running in docker container

The Pdf file is blank when I try to open to it from localhost/pdfs/filename.pdf, but when I do that for the picture it works (localhost/images/imagename.png) My app is running inside a docker container.当我尝试从 localhost/pdfs/filename.pdf 打开它时, Pdf 文件是空白的,但是当我为图片执行此操作时,它可以工作 (localhost/images/imagename.png) 我的应用程序在 docker 容器内运行。

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

   @Autowired
   private ApplicationEventPublisher publisher;

   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/").setCachePeriod(31556926);
    registry.addResourceHandler("/pdfs/**").addResourceLocations("classpath:/static/pdfs/").setCachePeriod(31556926);

   }
}

Both folders, images and pdfs, are inside src/main/resources/static folder.文件夹,图像和 pdf,都在src/main/resources/static文件夹中。

Why does it work for the image file but it doesn't work for pdf file?为什么它对图像文件有效,但对 pdf 文件无效? Thanks.谢谢。

I had the same problem - it was caused by maven filtering the resources.我遇到了同样的问题 - 它是由 maven 过滤资源引起的。 Excluding *.pdf helped solve the problem:排除 *.pdf 有助于解决问题:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 在 docker 容器中运行的应用程序无法访问 - Spring application running in docker container is not reachable 如何从Vaadin / Spring应用程序提供静态资源? - How to serve static resources from a Vaadin/Spring application? 连接被拒绝:访问在Docker容器中运行的Spring Boot应用程序 - Connection refused: accessing a spring boot application running in docker container 如何在单个 Docker 容器中提供多模块 spring 引导应用程序? - How can I serve a multi-module spring boot application in a single Docker Container? 从 docker 容器内运行的应用程序连接到本地运行的数据库 - Connect to database running in local from application running inside docker container Docker-compose - 为运行 spring boot 独立应用程序的 docker 容器提供基于 XML 的配置 - Docker-compose - Providing XML based configuration to docker container running spring boot standalone application 如何在具有来自 Controller 的自定义路径的 springboot 中提供 static 资源? - How to serve static resource in springboot having custom path from Controller? 在docker容器中重新部署spring-boot应用程序? - Redeploy spring-boot application in docker container? 如何在docker容器中运行的java maven项目中引用资源文件 - How to reference resource file in java maven project running in docker container 无法从 spring 启动 Z05B6053C41A2140AFD6FC3BE6Z 容器连接 mysql docker 容器 - not able to connect mysql docker container from spring boot docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM