首页 文章

Java - 无法通过url读取图像

提问于
浏览
0

我一直在寻找答案,没有任何帮助 .

那是我的代码:

BufferedImage img1 = ImageIO.read(new File(dir1));
URL url = new URL(image_srcURL);
BufferedImage img2 = 
ImageIO.read(url.openStream());

现在,当我尝试阅读网址时,我会收到以下异常 .

javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在java.base / sun.security中找到所请求目标的有效证书路径 . ssl.Alerts.getSSLException(Alerts.java:198)位于java.base / sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1974)的java.base / sun.security.ssl.Handshaker.fatalSE(Handshaker . java:345)java.base上的java.base / sun.security.ssl.Handshaker.fatalSE(Handshaker.java:339)java.base / sun.security.ssl.ClientHandshaker.checkServerCerts(ClientHandshaker.java:1968) /sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1777)位于java.base / sun.security.ssl.Handshaker的java.base / sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:264) .processLoop(Handshaker.java:1098)位于java.base / sun.security.ssl.Handshaker.processRecord(Handshaker.java:1026)java.base / sun.security.ssl.SSLSocketImpl.processInputReco rd(SSLSocketImpl.java:1137)位于java.base / sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1074)的java.base / sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973) java.base / sun.security上的java.base / sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1429)上的java.base / sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1402) .ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)位于java.base / sun.net.www.protocol的java.base / sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:567) .https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)位于java.base / sun.net.www.protocol的java.base / sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1581) .http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)at java.base / sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)at 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.doBuild(PKIXValidator.java:385)的java.base / sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:)中找到有效的证书路径到请求目标 . 290)在java.base / sun / sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:343)的java.base / sun / sun.security.validator.Validator.validate(Validator.java:264) .security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:226)at java.base / sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509) TrustManagerImpl.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.engineBuild(SunCertPathBuilder.java:126)中java.base / sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)中请求目标的路径在Java.base / java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297)java.base / sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)... 26更多

任何帮助都会很棒 . 我正在尝试读取网址中的图片并将其与现有文件进行比较 . 这里没有任何问题给出答案 .

Edit:

我尝试进入我的命令行“keytool -list -keystore keystore”,但它只显示了trustedCertEntries . 还没有解决方案 .

2 回答

  • 0

    下面是我如何实现它的一个例子 . 代码将响应主体读入awt BufferedImage ,然后写入.jpg文件,然后将文件读回字节数组,然后删除文件 . 我的回答很方便 . 我会尽力找到,所以我可以引用 . 希望它能帮到你!

    **编辑为@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);
    }
    }
    
  • 0

    首先,您需要获取服务器的证书,将其添加到keystone文件,或者添加到jre中的java信任库或单独的文件中 . 如果要创建新的信任库,请确保为程序提供信任库

相关问题