java.net.URL.getContent UnknownServiceException: no content-type

Viewed 652

Please help me to figure out the problem: We have 2 servers(old and new). And on the old server code works good, but on the new server this code doesn't work:

XWPFDocument doc;
try {
    doc = new XWPFDocument((InputStream) this.getClass().getClassLoader().getResource(filePath).getContent());
} catch (IOException e) {
    e.printStackTrace();
}

From logs:

java.net.UnknownServiceException: no content-type
        at java.net.URLConnection.getContentHandler(URLConnection.java:1241)
        at java.net.URLConnection.getContent(URLConnection.java:740)
        at java.net.URL.getContent(URL.java:1081)

The File path leads to docx file which is stored inside resources of one the modules of application. (And of course I've checked the existence of file.) So the UrlResource is

UrlResource:jar:file:/tomcat/webapps/ourApplication/WEB-INF/lib/moduleName.jar!/internalResourseFolderStructure/file.docx

About servers: They have a little bit different Tomcat versions: old - Tomcat 9.0.19.0 new - Tomcat 9.0.33.0

Maybe you can get me some tips how to debug it? (I am totally sure that it is something with server(Tomcat settings or something), but I don't know on this step how can debug such)

1 Answers

From URLConnection:

getContent

This method first determines the content type of the object by calling the getContentType method. If this is the first time that the application has seen that specific content type, a content handler for that content type is created.

As it's a file URL there are no HTTP headers, which is why the specific error is "no content-type". Even if the content type was available, Java by default has no idea how to turn a application/vnd.openxmlformats-officedocument.wordprocessingml.document into a Java object.

Perhaps on the old server Tomcat was set up with a MIME mapping for .docx and a ContentHandler registered to return something useful.

If what you actually want is an InputStream, then use getResourceAsStream, which will always work.

Related