网络图片下载

Copy
/** * 文件下载到指定路径 * * @param urlString 链接 * @param savePath 保存路径 * @param filename 文件名 * @throws Exception */ public static void download(String urlString, String savePath, String filename) throws IOException { // 构造URL URL url = new URL(urlString); // 打开连接 URLConnection con = url.openConnection(); // 设置请求超时为20s con.setConnectTimeout(20 * 1000); // 文件路径不存在 则创建 File sf = new File(savePath); if (!sf.exists()) { sf.mkdirs(); } // jdk 1.7 新特性自动关闭 try (InputStream in = con.getInputStream(); OutputStream out = new FileOutputStream(sf.getPath() + "\\" + filename)) { // 创建缓冲区 byte[] buff = new byte[1024]; int n; // 开始读取 while ((n = in.read(buff)) >= 0) { out.write(buff, 0, n); } } catch (Exception e) { e.printStackTrace(); } }
posted @   striver-sc  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示