Download image from internet

I am working on data capture using JDK URLConnection,when I try to download some image from a website,I found the image could not download in correct way. But I try to view those page in Browser(IE,Firefox),they are shown correctly. Below is the code:(I also have tried use HttpClient 3.1,same issue occured),any one can help on it?


    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();

Thanks and Regards

Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more