簡體   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