博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取网络资源保存本地
阅读量:4652 次
发布时间:2019-06-09

本文共 1240 字,大约阅读时间需要 4 分钟。

public void saveToFile(String destUrl) {       FileOutputStream fos = null;       BufferedInputStream bis = null;       HttpURLConnection httpUrl = null;       URL url = null;       intBUFFER_SIZE = 1024;       byte[] buf = newbyte[BUFFER_SIZE];       int size = 0;       try {           url = new URL(destUrl);           httpUrl = (HttpURLConnection) url.openConnection();           httpUrl.connect();           bis = new BufferedInputStream(httpUrl.getInputStream());           String fileName = destUrl.substring(destUrl.lastIndexOf("/"));           System.out.println(fileName);           fos = new FileOutputStream("e:\\haha.jpg");           while ((size = bis.read(buf)) != -1) {              fos.write(buf, 0, size);           }           fos.flush();       } catch (IOException e) {           e.printStackTrace();       } catch (ClassCastException e) {           e.printStackTrace();       } finally {           try {              fos.close();              bis.close();              httpUrl.disconnect();           } catch (IOException e) {              e.printStackTrace();           } catch (NullPointerException e) {              e.printStackTrace();           }       }    }

 

转载于:https://www.cnblogs.com/gavinYang/p/11196425.html

你可能感兴趣的文章
Modified判断Tedit TMemo类型的文件是否修改过
查看>>
python基础-对象
查看>>
如何使函数不生成执行代码
查看>>
MySQL 数据库设计 笔记与总结(3)物理设计
查看>>
第5周团队作业1:项目建议
查看>>
抠图划线
查看>>
HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)
查看>>
jmeter 参数化学习笔记
查看>>
Convert the AScii to SAC file
查看>>
PAT (Basic Level) Practise 1002. 写出这个数
查看>>
SxsTrace
查看>>
How to correctly use preventDefault(), stopPropagation(), or return false; on events
查看>>
How to: Update an .edmx File when the Database Changes
查看>>
纯CSS3绘制的猫咪老师——献给喜欢CSS3及《夏目友人帐》的你
查看>>
Mysql卸载
查看>>
Android事件分发机制
查看>>
linux之sleep
查看>>
JQuery绑定和注销事件
查看>>
搜索引擎易用性
查看>>
git的使用
查看>>