我正在尝试使用 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());
}
你有什么想法或建议吗?