如何创建 java.net.URL 来引用 CSS?可能吗?
我已经尝试了其他几种类似的方法来检查它是否是一个 css 页面,但它不起作用(虽然没有错误,但它没有):
int code = con.getResponseCode();
String type = con.getContentType();
con.disconnect();
//returns null if the connection had problems or its does not contain css
if (code != HttpURLConnection.HTTP_OK || !type.contains("stylesheet")) {
return null;
}
还有其他可能的解决方案吗?基本上我尝试做的是获取 css 页面并打印它。
最佳答案
以下面的代码为例
URL url = new URL("http://cdn.sstatic.net/stackoverflow/all.css?v=e97b23688ee8"); // some css on this site
HttpURLConnection con = (HttpURLConnection) url.openConnection();
Scanner scanner = new Scanner(con.getInputStream());
while(scanner.hasNextLine())
System.out.println(scanner.nextLine());
System.out.println(con.getContentType()); // prints text/css
您可能正在寻找错误的 Content-Type
。
关于java.net.URL 引用 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18497188/