簡體   English   中英

如何以正確的格式讀取網址?

[英]How to read url in correct format?

我正在嘗試讀取一個引發字符串的URL。 我將該字符串存儲在某個變量中,並嘗試使用jsp在我的網頁上打印該變量。 當我在網頁上打印字符串時,會出現一些垃圾字符。 如何獲得原始字符串?

這是我的jsp代碼:

Market.jsp

<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>

<%

    URL url;
    ArrayList<String> list1 = new ArrayList<String>();
    ArrayList<String> list2 = new ArrayList<String>();
    List commodity1 = null;
    List price1 = null;
    int c, p = 0;
    try {
        // get URL content

        String a = "http://122.160.81.37:8080/mandim/MarketWise?m=agra";
        url = new URL(a);
        URLConnection conn = url.openConnection();
        // open the stream and put it into BufferedReader
        BufferedReader br = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));

        StringBuffer sb = new StringBuffer();
        String inputLine;
        while ((inputLine = br.readLine()) != null) {
            System.out.println(inputLine);
            //  sb.append(inputLine);
            String s = inputLine.replace("|", "\n");
            s = s.replace("~", " ");
            StringTokenizer str = new StringTokenizer(s);
            while (str.hasMoreTokens())
            {
                String mandi = str.nextElement().toString();
                String price = str.nextElement().toString();
                list1.add(mandi);
                list2.add(price);
            }
        }
        commodity1 = list1.subList(0, 10);

        // commodity10=list1.subList(90,100);
        price1 = list2.subList(0, 10);

        int c1 = 0;
        int p1 = 0;
        for (c1 = 0, p1 = 0; c1 < commodity1.size() && p1 < price1.size(); c1++, p1++) {
            String x = (String) commodity1.get(c1);
            String y = (String) price1.get(p1);
            out.println(x);
            out.println(y);
        }

        br.close();

        //System.out.println(sb);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
%>


</body>
</html>

我得到以下輸出

धान 1325 चावल 2050 ज�वर 920 जौ 810 मकई 1280 गेहू� 1420 जो 1050 बेजर - जय 800 उड़द 3600

我如何實現我的期望目標?

提前致謝

我認為這是系統上的編碼問題。 我對JSP的了解還不足以告訴您什么,但是當在Linux上將代碼作為純Java應用程序運行並更改out.println();時,我就不會知道out.println(); 進入System.out.println(); 我可以看到預期的輸出。 (副節點:產品名稱是亞洲名稱,因此請不要感到驚訝。在這種情況下,這意味着這些字符與我對URL進行wget調用時的字符相同)。

這意味着:您的代碼很好:它加載了您想要的內容。 問題是演示。 HTML頁面具有自己的編碼。 我猜想JSP透明地做到了這一點(->這里我需要外部輸入如何執行此操作),但是結果必須具有以下三種解決方案之一:

  • 您的網頁采用西方編碼,並且不支持亞洲字符。 在這種情況下,您的字符串需要這樣編碼: &#8472; &#x2118;
  • 您的網頁是utf8或unicode編碼的,並且直接支持此字符
  • 即使在utf8編碼的頁面上,您也可以使用第一個示例中的編碼

無論選擇使用什么:輸出必須與格式匹配。 這也意味着您的代碼需要知道所選的字符集。 而且我確定JSP可以。 如果要使用默認實現的編碼,則需要為此找到一個函數。 看一下轉義JSP / Spring MVC中的所有字符串 這不能太難。

僅當您真的很瘋狂但不知道該怎么做時,才可以使用如下函數(這是一個hack!)對字符串進行編碼:

private String encode(String str) {
    StringBuilder sb = new StringBuilder();
    for (char ch : str.toCharArray())
        if (ch < 128)
            sb.append(ch);
        else {
            sb.append("&#x");
            String hx = Integer.toHexString(ch);
            while (hx.length() < 4)
                hx = "0" + hx;
            sb.append(hx);
            sb.append(";");
        }
    return sb.toString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM