您好我正在尝试使用简单的refular classm读取java中的url然后我得到一个字符串。我将该字符串存储在2个不同的变量中并打印该变量。当我打印该变量时,我在控制台上获得了一些印地语字体和英语。
当我尝试在我的jsp页面上使用相同的URL时,我只得到垃圾字符。我没有得到印地文字体。但是我想要在控制台上获得相同的输出。
这是我的
CommoditywiseGetUrl.java
public class CommoditywiseGetUrl {
public static void main(String args[]) {
URL url;
try {
// get URL content
String a = "http://122.160.81.37:8080/mandic/commoditywise?c=paddy";
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;
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
while ((inputLine = br.readLine()) != null) {
String s = inputLine.replace("|", "\n");
s = s.replace("~", " ");
//System.out.println(s);
StringTokenizer str = new StringTokenizer(s);
while (str.hasMoreTokens()) {
String mandi = str.nextElement().toString();
String price = str.nextElement().toString();
// System.out.println("Mandi:--->"+mandi);
//System.out.println("Price:--->"+price);
list1.add(mandi);
list2.add(price);
}
}
String item1 = null;
int i = 0;
int j = 0;
for (i = 0; i < list1.size() - 195; i++) {
System.out.println(list1.get(i));
}
for (j = 0; j < list2.size() - 195; j++) {
System.out.println(list2.get(j));
}
br.close();
//System.out.println(sb);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
My.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
URL url;
try {
// get URL content
String a = "http://122.160.81.37:8080/mandic/commoditywise?c=paddy";
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;
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
while ((inputLine = br.readLine()) != null) {
String s = inputLine.replace("|", "\n");
s = s.replace("~", " ");
//System.out.println(s);
StringTokenizer str = new StringTokenizer(s);
while (str.hasMoreTokens()) {
String mandi = str.nextElement().toString();
String price = str.nextElement().toString();
// System.out.println("Mandi:--->"+mandi);
//System.out.println("Price:--->"+price);
list1.add(mandi);
list2.add(price);
}
}
String item1 = null;
int i = 0;
int j = 0;
for (i = 0; i < list1.size() - 195; i++) {
out.println(list1.get(i));
}
for (j = 0; j < list2.size() - 195; j++) {
out.println(list2.get(j));
}
br.close();
//System.out.println(sb);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
%>
</body>
</html>
如何实现所需的输出?
提前致谢
答案 0 :(得分:0)
它看起来像是一个字符编码问题。
您可以按照JSP中的规定将字符编码为UTF-8,如下所示
Charset.forName("UTF-8").encode(myString)