java进:javaSE urlConnection.getInputStream() 乱码
代码如下:importjava.net.URL;importjava.net.URLConnection;importjava.util.*;importjava.io....
代码如下:import java.net.URL;import java.net.URLConnection;import java.util.*;import java.io.IOException;public class TestgetHtml { public static void main(String[] args) { StringBuffer allText=new StringBuffer(""); StringBuffer tempPage=new StringBuffer(""); String temper =""; String str=""; try { for(int i=1;i<10;i++){ URL url = new URL("http://某静态网页编码是utf-8.html"); URLConnection connection = url.openConnection(); connection.setRequestProperty("Content-Type", "text/html;charset=utf-8"); connection.connect(); // print header fields Scanner in = new Scanner(connection.getInputStream()); // print first ten lines of contents for (int n = 30; in.hasNextLine() && n <= 1000; n++) { tempPage.append(in.nextLine()); } // System.out.println("in"); //tempPage.subSequence(tempPage.indexOf("<tr"), tempPage.indexOf("</table>")); temper=tempPage.toString(); allText.append(temper); tempPage=new StringBuffer(""); } System.out.println(allText); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }目标页面使用的utf-8, java控制台程序是utf-8还是latin-1呢?如何获得网页上的中文数据,也就是如何解决这种乱码问题?
展开
1个回答
2013-11-06
展开全部
private String contentType = "text/xml;charset=utf-8";
private String cacheControl = "no-cache";
private boolean doOutPut = true;
private int connectTimeout = 5000;
public String getRs(String url) throws Exception {
if(null==url||url.equals(""))
throw new IllegalArgumentException("url参数为空");
StringBuilder resultXml = new StringBuilder();
try {
URLConnection urlConn = (new URL(url)).openConnection();
urlConn.setConnectTimeout(connectTimeout);
urlConn.setDoOutput(doOutPut);
urlConn.setRequestProperty("Content-Type", contentType);
urlConn.setRequestProperty("Cache-Control", cacheControl);
urlConn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
resultXml.append(line);
}
} catch (MalformedURLException mue) {
throw new IllegalArgumentException("url参数'"+url+"'解析失败:"+mue.getMessage());
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
throw e;
}
return resultXml.toString();
}
仅供参考 BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8"));
private String cacheControl = "no-cache";
private boolean doOutPut = true;
private int connectTimeout = 5000;
public String getRs(String url) throws Exception {
if(null==url||url.equals(""))
throw new IllegalArgumentException("url参数为空");
StringBuilder resultXml = new StringBuilder();
try {
URLConnection urlConn = (new URL(url)).openConnection();
urlConn.setConnectTimeout(connectTimeout);
urlConn.setDoOutput(doOutPut);
urlConn.setRequestProperty("Content-Type", contentType);
urlConn.setRequestProperty("Cache-Control", cacheControl);
urlConn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
resultXml.append(line);
}
} catch (MalformedURLException mue) {
throw new IllegalArgumentException("url参数'"+url+"'解析失败:"+mue.getMessage());
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
throw e;
}
return resultXml.toString();
}
仅供参考 BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8"));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询