2
我想使用的Java中的這個鏈接下載的文件...如何在java中下載給定url的文件?
http://translate.google.com/translate_a/ ...
如果你點擊上面的鏈接,你會得到一個JSON格式的文件。我想用java代碼來獲取這個文件。
我想這一個
String word = "Moon";
System.setProperty("http.proxyHost", "172.20.0.100");
System.setProperty("http.proxyPort", "8080");
URL url = new URL("http://translate.google.com/translate_a/t?client=t&text="+word+"&hl=en&sl=en&tl=hi&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1");
url.openConnection();
InputStream reader = url.openStream();
FileOutputStream writer = new FileOutputStream("t");
byte[] buffer = new byte[153600];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0)
{
writer.write(buffer, 0, bytesRead);
buffer = new byte[153600];
}
writer.close();
reader.close();
錯誤
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://translate.google.com/translate_a/…
at sun.net.www.protocol.http.HttpURLConnect…
at java.net.URL.openStream(URL.java:1031)
at smsMain.main(smsMain.java:19)
直接下載工作(只要按一下,看看「月」字的翻譯印地文),但通過程序它顯示的錯誤! !爲什麼?
請在這方面的幫助。 提前謝謝!
OP想知道_why_它可以從瀏覽器訪問,但不能從Java應用程序訪問。 –
因爲你必須[付費訪問](https://developers.google.com/translate/v2/pricing)到翻譯API – doublesharp
啊好的,你沒有寫完答案。 :) –