简体   繁体   中英

how to get file from this url , using java code?

To use google translate api I figured out this url

http://translate.google.com/translate_a/t?client=t&text=revenge&hl=en&sl=en&tl=hi&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1

If you click on it you will get a file in json format with accurate translation. To retrieve this file using java program I wrote following code.

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

But it shows following error

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for 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 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403) at java.net.URL.openStream(URL.java:1029) at smsMain.main(smsMain.java:20

Hope to get some help. Because we are getting file manually but using program it is forbidden.

From Google Translate home page:

Google Translate API is available as a paid service. See the Pricing and FAQ pages for details.

So getting an error is not really surprising.

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