Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请教大佬,通过chatGPT生成的图片,一般怎么下载呀,我通过URL下载的时候会报错 #4

Open
beifeng15 opened this issue Mar 10, 2023 · 7 comments

Comments

@beifeng15
Copy link

beifeng15 commented Mar 10, 2023

chatGPT生成的url为:
https://oaidalleapiprodscus.blob.core.windows.net/private/org-x2tDof6L8GQcT9HVev8uEEvq/user-QKO2z3TMxUYqYwTpR55INmEC/img-qBCyuxcVP2RHrpvmUUU3AjkP.png?st=2023-03-10T14%3A45%3A30Z&se=2023-03-10T16%3A45%3A30Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-03-10T03%3A35%3A18Z&ske=2023-03-11T03%3A35%3A18Z&sks=b&skv=2021-08-06&sig=lCj9fQwr6USg1ryf4wNB3HSRiAdpDE1XEmV9y9eKluQ%3D

Linux上的代码为:
`public static void download(String urlString, String filename, String savePath) {

    URL url = null;
    HttpsURLConnection con = null;
    try {
        url = new URL(urlString);
        try {
            // trust all hosts
            trustAllHosts();
            HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
            //如果请求是https,忽略证书校验
            if (url.getProtocol().toLowerCase().equals("https")) {
                https.setHostnameVerifier(DO_NOT_VERIFY);
                con = https;
            } else {
                con = (HttpsURLConnection) url.openConnection();
            }
            
            //设置请求超时为5s
            con.setConnectTimeout(50 * 1000);
            // 输入流
            InputStream is = con.getInputStream();
            
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            File sf = new File(savePath);
            if (!sf.exists()) {
                sf.mkdirs();
            }
        /* 获取图片的扩展名(我没用到,所以先注释了)
        String extensionName = urlString.substring(urlString.lastIndexOf(".") + 1);*/
            OutputStream os = new FileOutputStream(sf.getPath() + "/" + filename);
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            // 完毕,关闭所有链接
            os.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}`

报错信息如下:超时时间设置很长也没有作用
java.net.ConnectException: Connection timed out (Connection timed out)

@LiLittleCat
Copy link
Owner

LiLittleCat commented Mar 11, 2023

@beifeng15 你好,应该是路径的问题,我这边用你的代码跑出来是 403,直接访问链接显示没有权限:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:e1552f34-b01e-0023-7bc3-53a777000000 Time:2023-03-11T02:47:23.1914934Z</Message>
<AuthenticationErrorDetail>Signature not valid in the specified time frame: Start [Fri, 10 Mar 2023 14:45:30 GMT] - Expiry [Fri, 10 Mar 2023 16:45:30 GMT] - Current [Sat, 11 Mar 2023 02:47:23 GMT]</AuthenticationErrorDetail>
</Error>

代码结果:

java.io.IOException: Server returned HTTP response code: 403 for URL: https://oaidalleapiprodscus.blob.core.windows.net/private/org-x2tDof6L8GQcT9HVev8uEEvq/user-QKO2z3TMxUYqYwTpR55INmEC/img-qBCyuxcVP2RHrpvmUUU3AjkP.png?st=2023-03-10T14%3A45%3A30Z&se=2023-03-10T16%3A45%3A30Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-03-10T03%3A35%3A18Z&ske=2023-03-11T03%3A35%3A18Z&sks=b&skv=2021-08-06&sig=lCj9fQwr6USg1ryf4wNB3HSRiAdpDE1XEmV9y9eKluQ%3D
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1900)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
	at A.download(A.java:68)

由于不知道你代码里 DO_NOT_VERIFY 是什么,我换了一种方式忽略证书校验,代码如下:

 @Test
    public void test() {
        String property = System.getProperty("user.dir");
        String urlStr = "https://oaidalleapiprodscus.blob.core.windows.net/private/org-x2tDof6L8GQcT9HVev8uEEvq/user-QKO2z3TMxUYqYwTpR55INmEC/img-qBCyuxcVP2RHrpvmUUU3AjkP.png?st=2023-03-10T14%3A45%3A30Z&se=2023-03-10T16%3A45%3A30Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-03-10T03%3A35%3A18Z&ske=2023-03-11T03%3A35%3A18Z&sks=b&skv=2021-08-06&sig=lCj9fQwr6USg1ryf4wNB3HSRiAdpDE1XEmV9y9eKluQ%3D";
        String filename = "img-qBCyuxcVP2RHrpvmUUU3AjkP.png";
        download(urlStr, filename, property);

    }

    public static void download(String urlString, String filename, String savePath) {
        URL url = null;
        HttpsURLConnection con = null;
        try {
            url = new URL(urlString);
            try {
                HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
                //如果请求是https,忽略证书校验
                if (url.getProtocol().equalsIgnoreCase("https")) {
-                  https.setHostnameVerifier(DO_NOT_VERIFY);
+                  // 创建一个自定义的X509TrustManager
+                   TrustManager[] trustAllCerts = new TrustManager[]{
+                           new X509TrustManager() {
+                            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+                                  // do nothing
+                              }
+                              public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+                                  // do nothing
+                              }
+                              public X509Certificate[] getAcceptedIssuers() {
+                                  return new X509Certificate[0];
+                              }
+                           }
+                  };
+                  // 获取默认的SSLContext// 获取默认的SSLContext
+                  SSLContext sslContext = SSLContext.getInstance("SSL");
+                  sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
+                  // 设置当前线程的SSLContext为自定义的SSLContext
+                  HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
                    con = https;
                } else {
                    con = (HttpsURLConnection) url.openConnection();
                }

                //设置请求超时为5s
                con.setConnectTimeout(50 * 1000);
                // 输入流
                InputStream is = con.getInputStream();

                // 1K的数据缓冲
                byte[] bs = new byte[1024];
                // 读取到的数据长度
                int len;
                // 输出的文件流
                File sf = new File(savePath);
                if (!sf.exists()) {
                    sf.mkdirs();
                }
        /* 获取图片的扩展名(我没用到,所以先注释了)
        String extensionName = urlString.substring(urlString.lastIndexOf(".") + 1);*/
                OutputStream os = new FileOutputStream(sf.getPath() + "/" + filename);
                // 开始读取
                while ((len = is.read(bs)) != -1) {
                    os.write(bs, 0, len);
                }
                // 完毕,关闭所有链接
                os.close();
                is.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException | KeyManagementException e) {
                throw new RuntimeException(e);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

@beifeng15
Copy link
Author

@LiLittleCat 感谢大佬的热心解答,但应该不是url的问题。我使用Windows直接打开的时候,都能正常返回的

  1. 因为那个连接好像有一定的过期时间,过期之后,就不能直接打开了。
  2. 我尝试了很多中通过url下载文件的方法,但是只要是openai产出的图片,均会超时。
    image

所以咨询一下大佬,有没有更优雅的方式将产出的图片进行保存存储的呢。

@LiLittleCat
Copy link
Owner

@beifeng15 你的连接我用 WIndows 怎么打不开啊,:thinking:,如果是 HTTP 路径好像只有这种方式了吧。另外,你这个 host oaidalleapiprodscus.blob.core.windows.net在你的 Linux 机器上能访问吗?有没有加到 hosts 里。

@LiLittleCat
Copy link
Owner

@beifeng15 可以,这个我也能打开,那就是 hosts 的问题

@beifeng15
Copy link
Author

@LiLittleCat 这种有办法解决吗,其实我用了海外的机器去ping,也一样ping不通

@LiLittleCat
Copy link
Owner

@beifeng15 你用 curl 请求下图片路径,他能解析出 ip 应该不是 hosts 的问题,可能就是连不上

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants