我一直在尝试获取文件的MIME类型,然后检查文件是否是css之一,但它似乎不起作用。摘录如下:
String fileName = '/'+uri.getPath().substring(PATH.length());
URLConnection inputURL = getClass().getResource(fileName).openConnection();
InputStream inputStream = null;
try {
inputURL.setUseCaches(false);
inputStream = inputURL.getInputStream();
String mimeType = inputURL.getContentType();
if (inputStream != null) {
if (mimeType.equals("text/css")) {...}
else{...}...
答案 0 :(得分:0)
我认为如果您执行HTTP连接,将填写内容类型,例如
URLConnection c = new URL("http://foo/bar").openConnection();
String mimeType = c.getContentType();
在这种情况下,内容类型通过HTTP作为标头发送,并由适当的协议处理程序解析。我认为当从资源获得所谓的URLConnection时,这个属性根本没有被初始化。