久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java怎么根據(jù)url下載圖片

237次閱讀
沒有評論

共計 1075 個字符,預計需要花費 3 分鐘才能閱讀完成。

在 Java 中,可以使用 URLHttpURLConnection類來下載圖片。以下是一個簡單的示例代碼:

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ImageDownloader {public static void main(String[] args) {String imageUrl = "http://example.com/image.jpg";
        String destinationPath = "C:/path/to/save/image.jpg";
        
        try {URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            
            InputStream inputStream = connection.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            
            FileOutputStream fileOutputStream = new FileOutputStream(destinationPath);
            
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {fileOutputStream.write(buffer, 0, bytesRead);
            }
            
            fileOutputStream.close();
            bufferedInputStream.close();
            
            System.out.println("Image downloaded successfully.");
        } catch (IOException e) {e.printStackTrace();
        }
    }
}

只需將 imageUrl 替換為要下載的圖片的 URL,將 destinationPath 替換為要保存圖片的路徑,運行代碼即可下載圖片。

丸趣 TV 網(wǎng) – 提供最優(yōu)質的資源集合!

正文完
 
丸趣
版權聲明:本站原創(chuàng)文章,由 丸趣 2023-12-09發(fā)表,共計1075字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網(wǎng)絡搜集發(fā)布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 木里| 定州市| 前郭尔| 南宫市| 平顶山市| 漳浦县| 安乡县| 上虞市| 通渭县| 东明县| 芜湖县| 新建县| 花莲市| 嘉兴市| 彩票| 胶南市| 五华县| 马公市| 运城市| 邵武市| 博爱县| 绥江县| 桂东县| 鲁甸县| 罗甸县| 三江| 柞水县| 彰化县| 榆林市| 合水县| 资源县| 恩平市| 金门县| 南宫市| 普格县| 郓城县| 图木舒克市| 淮阳县| 玉龙| 台山市| 高雄县|