Docker中的Spring Boot静态文件夹
在Spring Boot项目中,我必须将默认位置更改为资源文件夹。 我使用这个代码来实现想要的结果:
@Bean WebMvcConfigurer configurer () { return new WebMvcConfigurerAdapter() { @Override public void addResourceHandlers (ResourceHandlerRegistry registry) { String location = "file:" + System.getProperty("user.dir") + "/pictures/"; registry.addResourceHandler("/pictures/**"). addResourceLocations(location); super.addResourceHandlers(registry); } }; }
在我的本地机器上翻转作品。 当创buildDocker镜像并运行它时,似乎没问题,但是当我尝试请求目录中的某些文件时,它会引发此exception:
2017-08-23 13:03:48.652 ERROR 8 --- [nio-8080-exec-1] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception java.net.UnknownHostException: pictures at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_144] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_144] at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_144] at sun.net.ftp.impl.FtpClient.doConnect(FtpClient.java:964) ~[na:1.8.0_144] at sun.net.ftp.impl.FtpClient.tryConnect(FtpClient.java:924) ~[na:1.8.0_144] at sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1019) ~[na:1.8.0_144] at sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1005) ~[na:1.8.0_144] at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:294) ~[na:1.8.0_144] ... more logs (not sure if they are needed to find the problem)
整个堆栈的例外。
我检查了图片文件夹是否在泊坞窗图像,是的,在那里。 我不知道是什么原因导致这个问题。 它可以是该file:
在文件path之前? 我在另一个函数中使用这个variablesSystem.getProperty("user.dir")
,它工作正常,所以它不能是问题。
Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp RUN mkdir /files ENV JAVA_OPTS="-Xmx512m -Xss512k" ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
当您执行资源的本地文件处理时,可以使用两种格式
file:abc/xyz
和file:///abc/xyz
。
file:abc/xyz
表示你想从相对pathabc/xyz
。
file:///abc/xyz
表示你想从绝对path/abc/xyz
。
在你的情况user.dir
可能是/
docker容器内的path作为file://pictures
/
file://pictures
。 正因为如此,FTP处理程序被Java激活