根据Excel批量下载图片
依赖包
<properties>
<poi.version>4.1.2</poi.version>
</properties>
<!-- excel工具 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>${poi.version}</version> </dependency>
下载图片
/** * 从网络Url中下载文件 * * @param urlStr * @param fileName * @param savePath * @throws IOException */ public static String downLoadFromUrl(String urlStr, String fileName, String savePath) { try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置超时间为3秒 conn.setConnectTimeout(3 * 1000); // 防止屏蔽程序抓取而返回403错误 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 得到输入流 InputStream inputStream = conn.getInputStream(); // 获取字节数组 byte[] getData = readInputStream(inputStream); // 文件保存位置 File saveDir = new File(savePath); if (!saveDir.exists()) { saveDir.mkdir(); } File file = new File(saveDir + File.separator + fileName); FileOutputStream fos = new FileOutputStream(file); fos.write(getData); if (fos != null) { fos.close(); } if (inputStream != null) { inputStream.close(); } // System.out.println("info:"+url+" download success"); return saveDir + File.separator + fileName; } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 从输入流中获取字节数组 * * @param inputStream * @return * @throws IOException */ public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return bos.toByteArray(); } public static void main(String[] args) throws Exception { File file = new File("C:\\Users\\admin\\Desktop\\urls.xlsx"); if (!file.exists()) { throw new Exception("文件不存在!"); } InputStream in = new FileInputStream(file); // 读取整个Excel XSSFWorkbook sheets = new XSSFWorkbook(in); // 获取第一个表单Sheet XSSFSheet sheetAt = sheets.getSheetAt(0); // //默认第一行为标题行,i = 0 // XSSFRow titleRow = sheetAt.getRow(0); // 循环获取每一行数据 for (int i = 0; i < sheetAt.getPhysicalNumberOfRows(); i++) { XSSFRow row = sheetAt.getRow(i); // 读取每一格内容 StringBuilder sb = new StringBuilder(); for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) { // XSSFCell titleCell = titleRow.getCell(index); XSSFCell cell = row.getCell(index); cell.setCellType(CellType.STRING); if (cell.getStringCellValue().equals("")) { continue; } sb.append(cell); } System.out.println(i + "\t" + sb); downLoadFromUrl(sb.toString(), i +".jpg", "E:\\img\\"); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人