URL 콘텐츠를 바이트 단위로 읽으 려합니다. 나는 url의 내용에서 64kb를 읽어야한다.URL 콘텐츠를 읽으십시오.
public void readUrlBytes(String address) {
StringBuilder builder = null;
BufferedInputStream input = null;
byte[] buffer = new byte[1024];
int i = 0;
try {
URL url = new URL(address);
URLConnection urlc = url.openConnection();
input = new BufferedInputStream(urlc.getInputStream());
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
builder.append(bytesRead);
if (i==64) {
break;
}
i++;
}
System.out.println(builder.toString());
} catch (IOException l_exception) {
//handle or throw this
} finally {
if (input != null) {
try {
input.close();
} catch(IOException igored) {}
}
}
}
위의 코드는 읽기 쉬운 문자입니다.
바이트를 읽어야합니다.
당신은 무엇을 물어? java.io.InputStream.read() 메소드는 스트림에서'byte '를 읽는다. –
붙여 넣은 새 코드를 살펴본 후 몇 가지 다른 문제가 있습니다. StrinngBuilder를 인스턴스화하지 않으므로 처음 사용하려고하면 NullPointerException이 발생합니다. 둘째로, append (byte [])를 호출 할 수없고 일어날 수있는 유용한 것을 기대할 수 없다. 바이트로 읽으려고한다고하지만, 모든 것이 끝난 후에 String을 얻으려는 것 같습니다. 이 바이너리 또는 문자 데이터를 읽으려고합니까? –