根据图片路径将文件下载到本地

import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class FileDownloader {
    public static void main(String[] args) {
        String fileUrl = "https://yztest-obs.botanee.com.cn/cart/4d59f2e3f96dd569dc23973d15e97b0ddf7acc77c2c10e117a8f0af4151d8464.png";
        String localFilePath = "downloaded_image.png"; // 指定本地保存路径
        
        try {
            // 创建URL对象
            URL url = new URL(fileUrl);
            
            // 下载文件到本地
            try (InputStream in = url.openStream()) {
                Files.copy(in, Paths.get(localFilePath), StandardCopyOption.REPLACE_EXISTING);
            }
            
            // 现在可以使用本地文件
            File file = new File(localFilePath);
            System.out.println("文件下载成功: " + file.getAbsolutePath());
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  

posted @ 2025-11-19 22:56  红尘沙漏  阅读(9)  评论(0)    收藏  举报