在Junit 和 Tomcat 中 运行同一段代码,出现编码错误
public class WeatherData {
// Junit 测试没有出现乱码,在Tomcat中运行出现乱码
public String getWeatherData(String url) throws IOException {
URL informationUrl = new URL(url);
InputStream inputStream = informationUrl.openStream();
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(isr);
byte[] bytes = br.readLine().getBytes("UTF-8");
String json = new String(bytes,"UTF-8");
br.close();
isr.close();
inputStream.close();
System.out.println(json);
return json;
}
@Test
public void TestGet() throws IOException{
System.out.println(getWeatherData("http://t.weather.sojson.com/api/weather/city/101260301"));
}
}
你是说的Tomcat 中server.xml 里面的: <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/> 我也是在网上搜索的,我试试