httpClient3.1を使用しています。length buffer.length
その間、a.txtにはすべてのコンテンツが含まれていると思います。しかし、そうではありません。使用する前getMethod.getResponseBodyAsStream
に、使用 getMethod.getResponseBodyAsString.everything
は良いです。例えば
public static void testGetResponseBodyAsStream() throws HttpException, IOException{
/*test stackoverflow.com*/
String uri="http://www.stackoverflow.com";
/*uses httpClient 3.1*/
HttpClient httpClient=new HttpClient();
GetMethod getMethod=new GetMethod(uri);
httpClient.executeMethod(getMethod);
/*get result write file */
FileOutputStream fileOutputStream=new FileOutputStream(new File("c:/a.txt"));
/*buffer is Large enough*/
byte[] buffer=new byte[1024*1024];
BufferedInputStream bufferedInputStream=new BufferedInputStream(getMethod.getResponseBodyAsStream());
/*While length<buffer.length then a.txt contains all of
content.But it's no.
Before uses getMethod.getResponseBodyAsStream, uses
getMethod.getResponseBodyAsString.everything is good . */
int length=bufferedInputStream.read(buffer,0,buffer.length);
fileOutputStream.write(buffer, 0, buffer.length);
fileOutputStream.close();
}