我正在使用HttpClient下载一些网上的图片,但是发现有些图片可以正确下载,有些图片则不可以。后来我尝试使用URLConnection下载,还是不能正确的下载,但是浏览器中是可以访问的。请大家帮忙看看,如下是一段测试性代码:
[code="java"]
URL server = new URL("http://203.98.189.57/images/product_images/Astrid%20Wallet_main.jpg");
HttpURLConnection connection = (HttpURLConnection)server.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.addRequestProperty("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, /");
connection.addRequestProperty("Accept-Language", "en-us,zh-cn;q=0.5");
connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727; MS-RTC LM 8)");
connection.connect();
InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream("c:/Astrid Wallet_main.jpg");
byte[] buffer = new byte[1024];
int byteReaded = is.read(buffer);
while(byteReaded != -1)
{
os.write(buffer,0,byteReaded);
byteReaded = is.read(buffer);
}
os.close();
[/code]
[b]问题补充:[/b]
应该就是这个问题,我晚上再尝试一下
不过os.flush()应该不需要,close之前,他会自动flush()的。
不过最好写上了,谢谢。