从网络上下载文件到本地
2015-08-10 13:34
621 查看
下载:
void downloadDemo() { String fileName = "E:" + File.separator + "test" + File.separator + "baidu.png"; URL url = null; HttpsURLConnection connection = null; File file = new File(fileName); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } InputStream is = null; OutputStream os = null; byte[] b = new byte[1024]; int len = 0; try { url = new URL("https://www.baidu.com/img/bdlogo.png"); connection = (HttpsURLConnection) url.openConnection(); is = connection.getInputStream(); os = new FileOutputStream(file); while ((len = is.read(b)) != -1) { os.write(b, 0, len); //System.out.println(len); } System.out.println("download complete!" + file.length() + " " + connection.getContentLength()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { is.close(); os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- TCP/IP,http,socket,长连接,短连接
- UNIX网络编程卷一:第八章 I/O UDP
- Android 网络:使用URLConnection提交请求,获取html代码加载WebView
- Android AsyncHttpClient图片上传
- (4.1.28.4)HTTP协议的报文浅析
- Jmeter使用SSL(HTTPS协议)
- Linux网络编程(2)——采用TCP的基本server的实现
- CloseableHttpClient加载证书来访问https网站
- 打造安全的App!iOS安全系列之 HTTPS
- 网络编程系列之十三 wireshark安装文件冲突
- Android Http传输数据
- 网络流24题 01 飞行员配对方案问题 (多源多汇点+最大流)
- 网站区别手机端和pc端用HttpContext.Current.Request.Url
- TCP/IP的网际层协议——ARP
- LESSON: HTTP 403 Error Caused By Spring Security Role Missing
- http://android.blog.51cto.com/268543/306424
- 读《http 权威指南后》,写的一个只有18K 大小的httpClient
- TCP/IP的网际层协议——ICMP
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- TCP三次握手、四次断开和香农定理