如果图像较新,如何从HTTP下载图像?
在这个问题中,我们可以从以下几个方面来回答:
Python示例代码:
import requests
import shutil
url = 'https://example.com/image.jpg'
response = requests.get(url, stream=True)
with open('image.jpg', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
Java示例代码:
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class DownloadImage {
public static void main(String[] args) {
try {
URL url = new URL("https://example.com/image.jpg");
InputStream in = new BufferedInputStream(url.openStream());
OutputStream out = new FileOutputStream("image.jpg");
for (int i; (i = in.read()) != -1;) {
out.write(i);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
总之,从HTTP下载图像可以使用多种方法和工具,具体实现方式取决于具体的需求和场景。
领取专属 10元无门槛券
手把手带您无忧上云