繁体   English   中英

java.net.URL引用CSS

[英]java.net.URL to refer to a CSS

如何创建一个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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM