Home > Mobile >  URLConnection.guessContentTypeFromName doesn't seem to work with csv extension
URLConnection.guessContentTypeFromName doesn't seem to work with csv extension

Time:06-22

I have been using URLConnection.guessContentTypeFromName(String fname) to detect what mime type I need to use in my headers to return different documents.

It was working fine until I tested with fname = "test.csv";. I can make it work with test.jpg or test.pdf but not with test.csv.

String fname = "test.csv";

String mimeType = URLConnection.guessContentTypeFromName(fname);

System.out.println(mimeType);

I have no issue getting the good mimetype for pdf and jpg extension but receive null with the csv one.

I can't seem to find any reason on google as to why it doesn't work. Is it normal behavior or am I missing something from the documentation ?

As a side note, I know of other ways to get the mime type from the filename but was wondering about this.

CodePudding user response:

A jvm brings along a list of file names and their mime types. For example openjdk specifies them for windows and linux.

You can override these definitions by specifying a jvm property content.types.user.table pointing to your definition of content-types.properties as specified by URLConnection#getFileNameMap().

Support for csv was only added recently with https://bugs.openjdk.org/browse/JDK-8273655

  • Related