我自己写了一段,但是得到的是乱码!privateStringgetPictureData(Stringpath)throwsException{URLurl=newURL(path);HttpURLConnectionconn=(HttpURLConnection)url.openConnection()...
我自己写了一段,但是得到的是乱码!
private String getPictureData(String path) throws Exception{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "text/html; charset=gb2312");
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();
byte[] data = readInputStream(inStream);
String html = new String(data);
return html;
}
private byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
展开