Debugging status=500 on url pointing into project [message #1839099] |
Sat, 13 March 2021 17:45  |
Eclipse User |
|
|
|
I have an image file that is stored in a file folder that is part of a Spring Tools project. My program is able to load a selected .png into this folder, and to delete files within this folder, but when I try to reference the file in this folder to display the image, I get a "status=500" error.
Any thoughts on how to go about debugging this?
My web server is Tomcat on Ubuntu 20.04.
The failing URL:
http://localhost:8080/ShopmeAdmin/user-photos/1/namhm.png
The error message (first few lines only, none of the lines refer to code that was written for the project):
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Mar 13 13:36:00 PST 2021
There was an unexpected error (type=Internal Server Error, status=500).
home
java.net.UnknownHostException: home
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)
at java.base/java.net.Socket.connect(Socket.java:648)
at java.base/sun.net.ftp.impl.FtpClient.doConnect(FtpClient.java:961)
at java.base/sun.net.ftp.impl.FtpClient.tryConnect(FtpClient.java:923)
at java.base/sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1018)
at java.base/sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1004)
at java.base/sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:319)
at java.base/sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:425)
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:186)
Image of screen with Package/ProjectExplorer showing location of folder "user-photos" in the project:

File location:
/home/phil/bin/SpringTools/workspace/ShopmeProject/ShopmeWebParent/ShopmeBackend/user-photos
When the project server is launched, the initial console comments include the following reference to home:
Starting ShopmeBackendApplication using Java 15.0.1 on UbuntuDesktop with PID 11957 (/home/phil/bin/SpringTools/workspace/ShopmeProject/ShopmeWebParent/ShopmeBackend/target/classes started by phil in /home/phil/bin/SpringTools/workspace/ShopmeProject/ShopmeWebParent/ShopmeBackend)
The following code was written to make the file location accessible:
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String dirName = "user-photos";
Path userPhotosDir = Paths.get(dirName);
String userPhotosPath = userPhotosDir.toFile().getAbsolutePath();
System.out.println("MvcConfig.userPhotosPath:" + userPhotosPath);
registry.addResourceHandler("/" + dirName + "/**")
.addResourceLocations("file:/" + userPhotosPath + "/");
}
}
The line that is output from the above follows:
MvcConfig.userPhotosPath:/home/phil/bin/SpringTools/workspace/ShopmeProject/ShopmeWebParent/ShopmeBackend/user-photos
The same git project works when run on a Windows system, so I'm wondering if there is something in how Eclipse or Spring or Tomcat is configured on Ubuntu that might be the cause the problem. I've searched for instances where the string "home" appears in the project, but the references are in the batch (bash) files "mvnw" or in a library that shouldn't matter (e.g., fontawesome). I am at a loss as to how to proceed.
|
|
|
|
Re: Debugging status=500 on url pointing into project [message #1839102 is a reply to message #1839100] |
Sat, 13 March 2021 20:22  |
Eclipse User |
|
|
|
Thank you! Your answer worked.
I didn't understand why this worked, so I spent some time searching for syntax info, and found the following:
https://en.wikipedia.org/wiki/File_URI_scheme
Quote:
A file URI takes the form of
file://host/path
where host is the fully qualified domain name of the system on which the path is accessible, and path is a hierarchical directory path of the form directory/directory/.../name. If host is omitted, it is taken to be "localhost", the machine from which the URL is being interpreted. Note that when omitting host, the slash is not omitted (while "file:///foo.txt" is valid, "file://foo.txt" is not, although some interpreters manage to handle the latter).
So, in the three-slash URL, the "host" part was omitted and "localhost" was presumed, and the position of the "localhost" would have been in between the 2nd and 3rd slashes.
After reading the above, I also tried the following and found that it also was successful.
registry.addResourceHandler("/" + dirName + "/**")
.addResourceLocations("file:" + userPhotosPath + "/");
I'm guessing that in this case the system again treats this as an "omitted host" situation and therefore make use of "localhost". Your three-slash form feels a little more explicit, so I'm going to chose it over the one-slash form.
|
|
|
Powered by
FUDForum. Page generated in 1.07770 seconds