网络图片下载
public static void main(String[] args) {
try {
String imageUrl = "https://wangyaow-1312039455.cos.ap-beijing.myqcloud.com/prescript/1704267810896.jpg"; // 网络图片URL
String fileName = imageUrl.substring(imageUrl.lastIndexOf("/")+1);
String destinationPath = "C:\\Users\\ASUS\\Desktop\\aa\\"+ fileName; // 本地文件路径
downloadImagess(imageUrl, destinationPath);
System.out.println("图片下载成功,保存路径:" + destinationPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void downloadImagess(String imageUrl, String destinationPath) throws IOException {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(destinationPath);
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
connection.disconnect();
}