【发布时间】:2015-09-17 16:04:01
【问题描述】:
我是 Java 的新手,也许我遗漏了一些东西,但我试图获取 url http://www.bunspace.com/static/photobucket/15155/dancing_buns.jpg 的内容类型。
我尝试了两种方式:
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();
两种方式都给了我结果“text/html; charset=ISO-8859-1”
显然url的类型是image/jpeg,我也用PHP检查过:
$type = get_headers("http://www.bunspace.com/static/photobucket/15155/dancing_buns.jpg", 1);
print($type['Content-Type']);
PHP 返回“image/jpeg”。
有没有办法以更可靠的方式在 Java 中获取 mime 类型?
【问题讨论】:
标签: java php mime-types