5

我正在尝试使用 Java 从 Google 翻译下载文本到语音。它适用于英语,但日语并不成功。以下是我的代码:

try{
            String word="〜のそばに";
            word=java.net.URLEncoder.encode(word, "UTF-8");
            URL url = new URL("http://translate.google.com/translate_tts?tl=ja&q="+word);
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            urlConn.addRequestProperty("User-Agent", "Mozilla/4.76");
            InputStream audioSrc = urlConn.getInputStream();
            DataInputStream read = new DataInputStream(audioSrc);
            OutputStream outstream = new FileOutputStream(new File("mysound.mp3"));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = read.read(buffer)) > 0) {
                    outstream.write(buffer, 0, len);                    
            }
            outstream.close();              
}catch(IOException e){
           System.out.println(e.getMessage());
}

你有什么想法或建议吗?

4

1 回答 1

6

看来您需要告诉 Google 搜索词包含 UTF-8 编码字符。

更改您的 URL 以http://translate.google.com/translate_tts?ie=UTF-8&tl=ja&q=解决我的问题。.mp3与谷歌翻译网站的音频翻译相比,我下载的内容相同。

于 2012-11-12T09:13:14.047 回答