Skip to content
Advertisement

How to not hardcoding image extensions in Java?

How to not hardcoding image extensions in Java?

JavaScript

Above is my code.

In the case statement, I have listed strings as hard-coded as jpg, jpeg….

This code is not flexible, so I’m sure there must be a better way.

I’d appreciate it if you could give me a solution that I’m not aware of.

Advertisement

Answer

As one of possible ways, you can use URLConnection.guessContentTypeFromName() like this.

JavaScript

You can add custom extensions to the $JAVA_HOME/jre/lib/content-types.properties file.

Also see these questions.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement