news 2026/5/30 18:32:33

33.批量通过GET链接下载图片到指定文件夹下

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
33.批量通过GET链接下载图片到指定文件夹下

通过JAVA循环调用照片下载链接

package cn.com.goldwind.ercp.fems.controller.common; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Arrays; import java.util.List; /** * 批量下载附件 * 图片名称为fileKey */ public class SimpleImageDownloader { // 配置超时时间(毫秒) private static final int CONNECT_TIMEOUT = 5000; private static final int READ_TIMEOUT = 10000; public static void main(String[] args) { // 1. 定义要下载的图片URL列表 List<String> imageUrls = Arrays.asList( "https://baidu.com/fems/file/download/aad4803c8cb1479e98215cbc1def9d24" ,"https://baidu.com/fems/file/download/2c25033166684effa09bb20ac71b38f5" ); String saveDir = "E:/downloaded_images"; // 保存目录 // 2. 循环执行下载 for (int i = 0; i < imageUrls.size(); i++) { String urlStr = imageUrls.get(i); try { System.out.println("正在下载第 " + (i + 1) + "/" + imageUrls.size() + " 张: " + urlStr); downloadFile(urlStr, saveDir); System.out.println("成功: " + urlStr); } catch (Exception e) { System.err.println("失败: " + urlStr + " - 错误信息: " + e.getMessage()); } } System.out.println("所有任务处理完毕。"); } /** * 下载单个文件 */ public static void downloadFile(String fileUrl, String saveDir) throws IOException { URL url = new URL(fileUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求属性 connection.setRequestMethod("GET"); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); // 模拟浏览器User-Agent,防止被某些服务器拦截 connection.setRequestProperty("User-Agent", "Mozilla/5.0"); // 检查响应码 int responseCode = connection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { throw new IOException("HTTP错误码: " + responseCode); } // 获取输入流 InputStream inputStream = connection.getInputStream(); // 生成文件名:从URL最后部分截取,如果无法获取则用时间戳 String fileName = getFileNameFromUrl(fileUrl); File dir = new File(saveDir); if (!dir.exists()) { dir.mkdirs(); } File outputFile = new File(dir, fileName); // 写入文件 try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } finally { inputStream.close(); connection.disconnect(); } } /** * 从URL中提取文件名 */ private static String getFileNameFromUrl(String urlStr) { String[] parts = urlStr.split("/"); String fileName = parts[parts.length - 1]; // 如果文件名不包含扩展名或为空,给予默认名 // if (fileName.isEmpty() || !fileName.contains(".")) { // fileName = "image_" + System.currentTimeMillis() + ".jpg"; // } fileName += ".jpg"; return fileName; } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/30 18:30:32

yt-dlp-gui:为Windows用户打造的专业视频下载图形界面解决方案

yt-dlp-gui&#xff1a;为Windows用户打造的专业视频下载图形界面解决方案 【免费下载链接】yt-dlp-gui Windows GUI for yt-dlp 项目地址: https://gitcode.com/gh_mirrors/yt/yt-dlp-gui 在视频内容日益丰富的数字时代&#xff0c;获取高质量视频资源的需求持续增长。…

作者头像 李华
网站建设 2026/5/30 18:30:23

从Maya/Max转Blender?教你用Python脚本快速上手骨骼动画系统

从Maya/3ds Max转向Blender&#xff1a;用Python脚本高效掌控骨骼动画当习惯了Maya的MEL或3ds Max的MaxScript后&#xff0c;Blender的Python API可能会让你既熟悉又陌生。作为一位从传统三维软件转型的开发者&#xff0c;我深刻理解这种"既视感"背后的认知落差——那…

作者头像 李华
网站建设 2026/5/30 18:19:58

基于ESP32与LVGL的嵌入式GUI开发:圣诞雪花球交互项目全解析

1. 项目概述与核心思路最近在捣鼓一个节日氛围小玩意儿&#xff0c;用Seeed Studio的XIAO ESP32S3和那块圆形的显示屏&#xff0c;做了一个可以交互的圣诞雪花球。这玩意儿摆在桌上&#xff0c;不仅有动态飘落的雪花&#xff0c;还能轻触屏幕切换不同的圣诞背景图&#xff0c;算…

作者头像 李华
网站建设 2026/5/30 18:17:58

3大核心模块深度解析:d2s-editor如何重塑你的暗黑2游戏体验

3大核心模块深度解析&#xff1a;d2s-editor如何重塑你的暗黑2游戏体验 【免费下载链接】d2s-editor 项目地址: https://gitcode.com/gh_mirrors/d2/d2s-editor 你是否曾在暗黑破坏神2中花费数百小时打造完美角色&#xff0c;却因一次意外丢失存档&#xff1f;或者想要…

作者头像 李华