Skip to content
This repository was archived by the owner on Jul 8, 2025. It is now read-only.
This repository was archived by the owner on Jul 8, 2025. It is now read-only.

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

@beifeng15

Description

@beifeng15

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions