我一直在到处寻找答案,但没有任何帮助。
这是我的代码:
BufferedImage img1 = ImageIO.read(new File(dir1));
URL url = new URL(image_srcURL);
BufferedImage img2 =
ImageIO.read(url.openStream());
现在,当我试图读取url时,我得到了下面的异常。
javax.net.ssl.ssl握手异常:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在中查找到请求目标的有效证书路径java.base/sun.security.ssl.Alers.getSSLException(Alerts.java:198)java.base/sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1974)位于java.base/sun.security.ssl.Handshaker.totalSE(Handshaker.java:345)位于java.base/sun.security.ssl.Handshaker.totalSE(Handshaker.java:339)在java.base/sun.security.ssl.ClientHandshaker.checkServerCerts(ClientHandshaker.java:1968)在java.base/sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1777)在java.base/sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:264)在java.base/sun.security.ssl.Handshaker.processLoop(Handshaker.java:1098)在java.base/sun.security.ssl.Handshaker.processRecord(Handshaker.java:1026)在java.base/sun.security.ssl.SSLSocketImpl.processInputRecord(SSLSocketImpl.java:1137)在java.base/sun.security.ssl.SLSocketImpl.readRecord(SSLSocketImpl.java:1074)在java.base/sun.security.ssl.SLSocketImpl.readRecord(SSLSocketImpl.java:973)在java.base/sun.security.ssl.SLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1402)在java.base/sun.security.ssl.SLSocketImpl.startHandshake(SSLSocketImpl.java:1429)在java.base/sun.security.ssl.SLSocketImpl.startHandshake(SSLSocketImpl.java:1413)在java.base/sun.net。www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:567)在java.base/sun.net。www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractdeleteHttpsURLConnection.java:185)在java.base/sun.net。www.protocol.HttpURLConnection.getInputStream0(HttpURLConnection.java:1581)在java.base/sun.net。www.protocol.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)在java.base/sun.net www.protocol.httpsURLConnection Impl.getInputStream(HttpsURLConnection impl.java:245)在com.folderName.Tools.DataForAutomationV1.compareImg(DFA.java:606)在com.folderName.VendorTests.SettingsPage.editCompDets(SettingsPage.java:562)在com.folderName.VendorTests.SettingsPage.runTest(SettingsPage.java:62)网址:com.folderName:Vendor.vendorTests(Vendor.java:36)com.folderName.Main.Main(Main.java:40)原因:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在中查找到请求目标的有效证书路径java.base/sun.security.validator.PIXValidator.doBuild(PKIXValidator.java:385)在java.base/sun.security.validator.PIXValidator.engineValidate(PKIXValidator.java:290)在java.base/sun.security.validator.validate(validator.java:264)在java.base/sun.security.ssl.X509TrustManagerImpl.validate(X509CrustManagerImpl.java:343)在java.base/sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509CrustManagerImpl.java:226)在java.base/sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:133)在java.base/sun.security.ssl.ClientHandshaker.checkServerCerts(ClientHandshaker.java:1947)…20多个原因:sun.security.provider.certpath.SunCertPathBuilderException:无法在中查找到请求目标的有效证书路径java.base/sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuildr.java:141)在java.base/sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuildr.java:126)在java.base/java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297)在java.base/sun.security.validator.PIXValidator.doBuild(PKIXValidator.java:380)…还有26个
任何帮助都会很棒。我正在尝试读取url中的图像,并将其与现有文件进行比较。这里没有任何问题可以给出答案。
编辑:
我尝试在命令行中输入"keytool-list-keystore-keystore",但它只显示了trustedCertEntries。目前还没有解决方案。
下面是我如何实现它的一个例子。代码将响应体读取到awtBufferedImage
中,然后写入.jpg文件,然后将文件读取回字节数组,然后删除该文件。我确信我在SO上找到了帮助,但我手头没有答案。我会努力找到它,以便引用。希望它能帮助你!
**正如@Srikanthkumar在回答中提到的那样进行编辑,您还需要获得证书并将其添加到JVM的信任库中。你可以通过浏览器通过任何你有证书探索工具的浏览器来获取。
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.util.Base64;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Override
public String getImage(String productNumber) throws EcomWebException {
try {
BufferedImage image = ImageIO.read(url);
File file = new File(guid.toString() + ".jpg");
ImageIO.write(image, "jpg", file);
String base64ImageString = Base64.getEncoder().encodeToString(Files.readAllBytes(file.toPath()));
file.delete();
return base64ImageString;
} catch (Exception e) {
throw new EcomWebException(IMAGE_NOT_FOUND_MESSAGE + productNumber, e);
}
}
首先,您需要获得服务器的证书,将其添加到keystone文件中,或者添加到jre中的java信任库中,或者单独的文件中。如果你正在创建一个新的信任库,那么请确保你为你的程序提供信任库
本文中的以下片段可能会对您有所帮助:
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType
) {}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType
) {}
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (GeneralSecurityException e) {
}