gpt4 book ai didi

java - urlconnection javax.net.ssl.SSLException : java. lang.IllegalStateException

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:48 24 4
gpt4 key购买 nike

我正在尝试使用 UrlConnection 发送音频(表示为 base64 字节数组 - 根据 Google Cloud Speech API 的请求)。我正在使用 ubuntu 在 Raspberry Pi 上执行此操作。如果我在我的笔记本电脑上使用它(也使用 ubuntu),连接确实有效。我正在使用带有 URLConnection 的 java。崩溃发生在 OutputStream out = urlConnection.getOutputStream();

Java 代码:

// URL to google + parameter with the API key.
URL googleUrl = new URL(GOOGLE_RECOGNIZER_URL + "?key=" + APIKEY);

// Open New URL connection channel.
URLConnection urlConnection;

urlConnection = googleUrl.openConnection();

// we want to do output. (input is automaticly set on true).
urlConnection.setDoOutput(true);

// No caching.
urlConnection.setUseCaches(false);

// Add headers.
urlConnection.setRequestProperty("Content-Type", "application/json");

// For sending the recorded data to google it needs to be first
// converted to a byte array.
byte[] array1 = AudioToByteArray.readAudioFileData(file);

// And next convert the byte array to a base64 string.
String audioString = Base64.getEncoder().encodeToString(array1);

// Make (JSON) Strings for configuration and location of audio file.
String testConfig = "\"config\": { \"encoding\":\"LINEAR16\", \"sampleRate\": 16000, \"languageCode\": \"nl-NL\"}";
String testAudioLoc = "\"audio\": { \"content\": \"" + audioString + "\" }";
String together = "{ " + testConfig + "," + testAudioLoc + " }";

// Send the string to google.
OutputStream out = urlConnection.getOutputStream();

OutputStreamWriter wr = new OutputStreamWriter(out);
wr.write(together);
wr.flush();
wr.close();
out.close();

错误:

javax.net.ssl.SSLException: java.lang.IllegalStateException
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1906)
at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1889)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1410)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1316)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1291)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at main.ContactGoogleCloudSpeechAPI.writeOut(ContactGoogleCloudSpeechAPI.java:98)
at main.ContactGoogleCloudSpeechAPI.contactGoogleUrlCon(ContactGoogleCloudSpeechAPI.java:41)
at main.SpeechRecognition.contactAPI(SpeechRecognition.java:184)
at main.SpeechRecognition.mainLoop(SpeechRecognition.java:104)
at main.SpeechRecognition.run(SpeechRecognition.java:81)
at main.SpeechRecognition.main(SpeechRecognition.java:62)

最佳答案

我遇到了同样的异常,并意识到我正在使用 openJDK

所以解决办法就是卸载openJDK,安装oracle JDK

您可以使用以下命令检查您使用的是哪个 JDK 版本:

java -version

关于java - urlconnection javax.net.ssl.SSLException : java. lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41637874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com