我试图在代理服务器后面的URL处下载图像。请注意,URL不以.jpg或.png结尾。如果通过浏览器打开并且图像可见。但使用以下代码时,我无法保存/下载它。
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");
我收到以下错误:
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)
答案 0 :(得分:1)
您可能需要验证您的请求。 Google云端硬盘使用OAuth2:
此处提供了使用Google Client library下载文件的工作示例: