chatgpt-java
chatgpt-java copied to clipboard
请教大佬,通过chatGPT生成的图片,一般怎么下载呀,我通过URL下载的时候会报错
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)
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)
gpt生成的图片可能在国内访问会很慢甚至访问不了。 你可以上传到oss上,做云存储生成链接,然后既可以下在也可以做前端展示。 可以参考下阿里云oss,或者七牛云
@beifeng15
下载都下载不下来,你怎么上传到oss上?
使用hutool的HttpUtil.downloadFile可以下载
下载都下载不下来,你怎么上传到oss上?
直接传流过去