如何使用java获取非英语文本作为响应
我从维基百科页面得到响应,并将响应粘贴到html文件中。如果我在浏览器中打开html文件,我将无法获得除英语以外的其他语言(我使用UTF-8)。我附上图片的语言在html 我尝试了几种方法来使用java获得响应,如下所示 方式一,如何使用java获取非英语文本作为响应,java,utf-8,response,Java,Utf 8,Response,我从维基百科页面得到响应,并将响应粘贴到html文件中。如果我在浏览器中打开html文件,我将无法获得除英语以外的其他语言(我使用UTF-8)。我附上图片的语言在html 我尝试了几种方法来使用java获得响应,如下所示 方式一, URL url = new URL ("https://en.wikipedia.org/wiki/Sachin_Tendulkar"); byte[] encodedBytes = Base64.encodeBase64("root:pass".g
URL url = new URL ("https://en.wikipedia.org/wiki/Sachin_Tendulkar");
byte[] encodedBytes = Base64.encodeBase64("root:pass".getBytes());
//System.out.println("Host --------"+url.getHost());
String encoding = new String (encodedBytes);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
connection.setDoInput (true);
connection.setRequestProperty ("Authorization", "Basic " + encoding);
connection.connect();
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
String s = line.toString();
System.out.println(s);
}
我还尝试了下面的代码,但这也没有显示字体,因为它是在
维基
几点:
- 您的代码没有显示如何准确地持久化对HTML文件的响应。您只是将流程的标准输出重定向到一个文件吗?确保即使在写入输出文件时也使用UTF-8
- 为什么在读取循环的每次迭代中都要对整个StringBuffer实例执行System.out.println
- 为什么调用line.getBytes()而从不使用输出
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HtmlDownloader {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String ENCODING = "UTF-8";
public boolean download(String urlAddress, String outputFileName) {
HttpURLConnection con = null;
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
URL url = new URL(urlAddress);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Charset", ENCODING);
is = new BufferedInputStream(
con.getInputStream()
);
os = new BufferedOutputStream(
new FileOutputStream(outputFileName)
);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public static void main(String[] args) {
HtmlDownloader d = new HtmlDownloader();
if (d.download("https://en.wikipedia.org/wiki/Sachin_Tendulkar", "c:\\wiki.html"))
System.out.println("SUCCESS");
else
System.out.println("FAIL");
}
}
现在,我只是复制控制台(即html响应)并将其粘贴到编辑器中,然后将其另存为html。然后我在浏览器中打开html文件。这就是我制作sysout的原因。最后一点,我从这个问题中得到了参考,我怀疑编码问题可能是由剪贴板操作或用于保存文件的编辑器配置引起的。试着从Java保存数据,看看它是否有效。我用另一种方法更新了我的问题。请看一下,使用
编码这样的变量是个不错的主意,但是您应该一致地使用它,即指定它为“Accept Charset”
,而不是将文本“UTF-8”
保留在那里。但是,当您使用相同的编码进行读写时,双重转换变得毫无意义。只需将字节直接从InputStream
复制到OutputStream
,而无需进行不必要的转换…顺便说一句,如果目标是文件,您需要该.Windows/Linux/OS X/IDE控制台吗?控制台上是否正确显示了非ASCII字符?当您在UTF-8
中请求数据时,也应使用该编码new InputStreamReader(content)
使用系统中的任何默认编码。使用新的InputStreamReader(内容为“UTF-8”)
。当您有一个字符串时,请将其按原样附加到StringBuilder
中,而不是将其转换为字节[]
数组…
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HtmlDownloader {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String ENCODING = "UTF-8";
public boolean download(String urlAddress, String outputFileName) {
HttpURLConnection con = null;
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
URL url = new URL(urlAddress);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Charset", ENCODING);
is = new BufferedInputStream(
con.getInputStream()
);
os = new BufferedOutputStream(
new FileOutputStream(outputFileName)
);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public static void main(String[] args) {
HtmlDownloader d = new HtmlDownloader();
if (d.download("https://en.wikipedia.org/wiki/Sachin_Tendulkar", "c:\\wiki.html"))
System.out.println("SUCCESS");
else
System.out.println("FAIL");
}
}