为了使用谷歌翻译API,我找到了这个网址
如果您点击它,您将获得一个带有准确翻译的 json 格式的文件。 为了使用 java 程序检索此文件,我编写了以下代码。
String word = "revenge";
System.setProperty("http.proxyHost", "172.30.0.16");
System.setProperty("http.proxyPort", "3128");
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();
但它显示以下错误
线程“main”中出现异常 java.io.IOException:服务器返回 HTTP 响应代码:403,URL:http://translate.google.com/translate_a/t?client=t&text=Moon&hl=en&sl=en&tl=hi&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403) 在 java.net.URL.openStream(URL.java:1029) 在 smsMain.main(smsMain.java:20
希望能得到一些帮助。因为我们是手动获取文件,但使用程序是禁止的。
最佳答案
来自Google Translate主页:
Google Translate API is available as a paid service. See the Pricing and FAQ pages for details.
因此出现错误并不奇怪。
关于java - 如何使用java代码从这个url获取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13101678/