zoukankan      html  css  js  c++  java
  • 字节流和字符流完成URL下载,并存入本地

    字节流

     1 package download;
     2 
     3 import java.io.BufferedInputStream;
     4 import java.io.BufferedOutputStream;
     5 import java.io.File;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 import java.net.HttpURLConnection;
     9 import java.net.URL;
    10 
    11 public class ByteDownload extends Thread {
    12     private String down_load;
    13     private String path;
    14     
    15     public ByteDownload(String down_load, String path) {
    16         super();
    17         this.down_load = down_load;
    18         this.path = path;
    19     }
    20 
    21     @Override
    22     public void run() {
    23         BufferedInputStream bis=null;
    24         BufferedOutputStream bos=null;
    25         try {
    26             URL url=new URL(down_load);
    27             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    28             conn.connect();
    29             bis=new BufferedInputStream(conn.getInputStream());
    30             bos=new BufferedOutputStream(new FileOutputStream(new File(path)));
    31             byte[] data=new byte[1024];
    32             int len;
    33             if(conn.getResponseCode()==200){
    34                 while((len=bis.read(data))!=-1){
    35                     bos.write(data, 0, len);
    36                     bos.flush();
    37                 }
    38             }
    39             System.out.println("下载成功");
    40         } catch (Exception e) {
    41             e.printStackTrace();
    42         }finally{
    43             try {
    44                 bis.close();
    45                 bos.close();
    46             } catch (IOException e) {
    47                 e.printStackTrace();
    48             }
    49         }
    50         
    51     }
    52 }

    字符流

     1 package download;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.BufferedWriter;
     5 import java.io.File;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 import java.io.InputStreamReader;
     9 import java.io.OutputStreamWriter;
    10 import java.net.HttpURLConnection;
    11 import java.net.URL;
    12 
    13 public class CharDownload extends Thread {
    14     private  String down_url;
    15     private String path;
    16     
    17     public CharDownload(String down_url, String path) {
    18         super();
    19         this.down_url = down_url;
    20         this.path = path;
    21     }
    22     @Override
    23     public void run() {
    24         BufferedReader br=null;
    25         BufferedWriter bw=null;
    26         try {
    27             URL url=new URL(down_url);
    28             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    29             conn.connect();
    30             br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
    31             bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path))));
    32             String str = null;
    33             if(conn.getResponseCode()==200){
    34                 str=br.readLine();
    35                 System.out.println(str);
    36                 bw.write(str);
    37             }
    38             ByteDownload bd=new ByteDownload(str, "d:/folder01/99.jpg");
    39             bd.start();
    40         } catch (Exception e) {
    41             e.printStackTrace();
    42         }finally{
    43             try {
    44                 br.close();
    45                 bw.close();
    46             } catch (IOException e) {
    47                 e.printStackTrace();
    48             }
    49             
    50         }
    51     }
    52 
    53 }
  • 相关阅读:
    [转]关于WM_NCHITTEST消息
    微信小程序的年月日-年月日选择器基于picker的mode = multiSelector日期范围
    解决移动端浏览器 HTML 音频不能自动播放的三种方法
    小程序反编译
    CSS实现背景透明,文字不透明
    event.target 属性返回哪个 DOM 元素触发了事件。
    微信小程序去除button边框
    jQuery setInterval倒计时精确到毫秒
    获取openid [微信小程序]
    array_column() 函数[二维数组转为一维数组]
  • 原文地址:https://www.cnblogs.com/String-likainian/p/5855201.html
Copyright © 2011-2022 走看看