• 字节流和字符流完成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 }
  • 相关阅读:
    商品列表双向联动 better-scroll
    【2020省选模拟】题解
    【洛谷 P3643】【APIO2016】 划艇(DP)
    【洛谷 P4384】 [八省联考2018]制胡窜(SAM / 线段树合并)
    【BZOJ #2034】 [2009国家集训队]最大收益(贪心 / 匈牙利算法)
    【BZOJ #4977】【[Lydsy1708月赛】 跳伞求生(模拟费用流)
    【洛谷 P5825】 排列计数(二项式反演 / 多项式 / 生成函数)
    【2020省选模拟】题解
    【LOJ #6068】「2017 山东一轮集训 Day4」棋盘(费用流)
    【洛谷 P4564】 【CTSC2018】假面(概率DP)
  • 原文地址:https://www.cnblogs.com/String-likainian/p/5855201.html
Copyright © 2020-2023  润新知