简体   繁体   中英

Unable to Download Image behind Proxy Server

I was trying to Download an image present at this URL behind a proxy server. Note that the URL does not end with .jpg or .png . If opened through the browser and the image is visible. But while using the following code i am unable to save/download it.

int pageno = 1; 
     System.setProperty("http.proxyHost", "10.3.100.207");
     System.setProperty("http.proxyPort", "8080");
     System.setProperty("https.proxyHost", "10.3.100.207");
     System.setProperty("https.proxyPort", "8080");
     while(pageno < 50)
     {       
         String fileName = String.format("%d",pageno); 
         URL link = new URL("https://drive.google.com/viewerng/img?id=ACFrOgAJjqc29Qp5SCU7TFHN7pEskq8BQPXHZxaORsnhS5LW1Nl1ypxZTiW0Kylbltw3Z0XkMsACVpvmQWyHef3HdXB-XDXduE1HMXblNAMeZ2N5opRAViKbl8GrAYA=&u=0&w=800&page="+pageno);              
         URLConnection myconn = link.openConnection();
         myconn.setRequestProperty ("User-agent", "Mozilla/5.0");
         InputStream in = myconn.getInputStream();
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         byte[] buf = new byte[1024];
         int n = 0;
         while (-1!=(n=in.read(buf)))
         {
            out.write(buf, 0, n);
         }
         out.close();
         in.close();
         byte[] response = out.toByteArray();
         FileOutputStream fos = new FileOutputStream(fileName);
         fos.write(response);
         fos.close();
         System.out.println(pageno);
         pageno++;
     }
     System.out.println("Finished");

I get the following error:

 Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://drive.google.com/viewerng/img?id=ACFrOgAJjqc29Qp5SCU7TFHN7pEskq8BQPXHZxaORsnhS5LW1Nl1ypxZTiW0Kylbltw3Z0XkMsACVpvmQWyHef3HdXB-XDXduE1HMXblNAMeZ2N5opRAViKbl8GrAYA=&u=0&w=800&page=1
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at ml.DownloadFile.main(DownloadFile.java:31)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM