yuriscom
yuriscom

Reputation: 650

Detecting mime type in Java, wrong results

I'm sort of newbie in Java, maybe i'm missing something, but i tried to get the content type of the url http://www.bunspace.com/static/photobucket/15155/dancing_buns.jpg.

I tried in 2 ways:

1:

URL url = new URL(path);
URLConnection urlConnection = url.openConnection();
return urlConnection.getContentType();

2:

URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection)  url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
return connection.getContentType();

both ways gave me result "text/html; charset=ISO-8859-1"

Obviously the type of the url is image/jpeg, and i also checked with PHP:

$type = get_headers("http://www.bunspace.com/static/photobucket/15155/dancing_buns.jpg", 1);
print($type['Content-Type']);

PHP returned "image/jpeg".

Is there a way to get mime type in Java in more trustful way?

Upvotes: 1

Views: 434

Answers (1)

RealSkeptic
RealSkeptic

Reputation: 34628

That site seems to reject the default Java user agent, which is "Java/1.7" (or whatever version you use). Some sites do that to avoid trivial bots.

So you need to set the user agent string - so to extend your second method:

URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection)  url.openConnection();
connection.setRequestProperty("User-Agent", "Not a Java Bot");
connection.setRequestMethod("HEAD");
connection.connect();
return connection.getContentType();

This will return image/jpeg from the aforesaid URL.

Of course, you can use the user agent string of a real browser if you don't want your access to be noticed.

Upvotes: 3

Related Questions