怎么用url写一个下载图片

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【zq620901】截止到2008-07-09 16:48:29的历史汇总数据(不包括此帖):
    发帖的总数量:0                        发帖的总分数:0                        每贴平均分数:0                        
    回帖的总数量:0                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:---------------------结分的百分比:---------------------
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   


    URL ?
    Runtime.getRuntime() 好了.
    启动迅雷下,嘿嘿!愿楼主好运噢!!
      

  3.   

    IO学的不太好,给个程序你参考一下吧/**
    * picture 是图片地址
    * dest 是保存图片的地址
    **/
    public class GetPicture {
    public static void getPicture(String picture, String dest) {
    try {
    URL url = new URL(picture);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    DataInputStream reader = new DataInputStream(con.getInputStream());

    DataOutputStream writer = new DataOutputStream(new FileOutputStream(dest));
    int ks = 0;
    while((ks = reader.read()) != -1) {
    writer.write(ks);
    }
    reader.close();
    writer.close();
    } catch (FileNotFoundException ex) {
    System.out.println("NOT FOUND:" + picture);
    } catch (ConnectException ex) {
    System.out.println("Timeout:" + picture);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    }
      

  4.   

    3楼正解,如果把关闭输入输出流的 close 方法放到 finally 块里就更完美了。
      

  5.   

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.SocketTimeoutException;
    import java.net.URL;public class URLtest { public static void main(String[] args) {
    String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
    String svpath = "C:/ZCG/downloads/dimg.gif";
    try {
    URL url = new URL(imgurl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    System.out.println("ResponseCode = " + con.getResponseCode());
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
    bis = new BufferedInputStream(con.getInputStream());
    bos = new BufferedOutputStream(new FileOutputStream(svpath));
    byte[] bs = new byte[100];
    int n = 0;
    while ((n = bis.read(bs)) != -1) {
    bos.write(bs, 0, n);
    }
    bos.flush();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (bis != null) {
    try {
    bis.close();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    bis = null;
    }
    }
    if (bos != null) {
    try {
    bos.close();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    bos = null;
    }
    }
    }
    con.disconnect();
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (SocketTimeoutException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }}
      

  6.   

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.SocketTimeoutException;
    import java.net.URL;public class ImgDlder { public static void main(String[] args) {
    String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
    String svpath = "C:/downloads/dimg.gif";
    try {
    URL url = new URL(imgurl);
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    System.out.println("ResponseCode = " + con.getResponseCode());
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
    bis = new BufferedInputStream(con.getInputStream());
    bos = new BufferedOutputStream(new FileOutputStream(svpath));
    byte[] bs = new byte[100];
    int n = 0;
    while ((n = bis.read(bs)) != -1) {
    bos.write(bs, 0, n);
    }
    bos.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (bis != null) {
    try {
    bis.close();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    bis = null;
    }
    }
    if (bos != null) {
    try {
    bos.close();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    bos = null;
    }
    }
    }
    con.disconnect();
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (SocketTimeoutException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }}
      

  7.   

    完善
          // 增加referer参数,用于下载那些防止盗链的图片
          con.addRequestProperty("referer",picture);
      

  8.   

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.SocketTimeoutException;
    import java.net.URL;public class ImgDlder {    public static void main(String[] args) {
            String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
            String svpath = "C:/downloads/dimg.gif";
            try {
                URL url = new URL(imgurl);
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
                System.out.println("ResponseCode = " + con.getResponseCode());
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                    bis = new BufferedInputStream(con.getInputStream());
                    bos = new BufferedOutputStream(new FileOutputStream(svpath));
                    byte[] bs = new byte[100];
                    int n = 0;
                    while ((n = bis.read(bs)) != -1) {
                        bos.write(bs, 0, n);
                    }
                    bos.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            bis = null;
                        }
                    }
                    if (bos != null) {
                        try {
                            bos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            bos = null;
                        }
                    }
                }
                con.disconnect();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SocketTimeoutException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }}
    sagezk  终于升了   得散点分阿 :)