Reputation: 845
In Spring Boot project I have to change the default location to the resource folder. I use this code to achieve the wanted results:
@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);
}
};
}
On my local machine everthing works. When create the docker image and run it everyhing seems to be ok but when I try to request some of the files in the directory it throws this exception:
2017-08-23 13:03:48.652 ERROR 8 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[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)
The whole stack of the exception.
I checked if the the pictures folder is in the docker images and yes its there. I am not sure what causes this problem. Can it be that file:
before the file path? I use this variable System.getProperty("user.dir")
in another function and it works properly so it mustn't be the problem.
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" ]
Upvotes: 2
Views: 3087
Reputation: 146490
There are two formats that can be used when you do local file handling of resources
file:abc/xyz
and file:///abc/xyz
.
file:abc/xyz
means you want to serve from a relative path abc/xyz
.
file:///abc/xyz
means you want to serve from an absolute path /abc/xyz
.
In your case user.dir
was probably /
inside the docker container making the path as file://pictures
. Because of which the FTP handler was being activated by Java
Upvotes: 4