怎么根据一个url读取图片,然后成为一个BufferedImage对象
比如这个是读取文件的BufferedImage image = ImageIO.read(imageFile);
那么怎么读取url,请指点一下,谢谢

解决方案 »

  1.   

    public   String   saveToFile(String   destUrl,   String   pathName)   { 
                    FileOutputStream   fos   =   null; 
                    BufferedInputStream   bis   =   null; 
                    HttpURLConnection   httpUrl   =   null; 
                    URL   url   =   null; 
                    String   fullPath   =   null; 
                    
                    byte[]   buf   =   new   byte[BUFFER_SIZE]; 
                    int   size   =   0; 
                                  
                    try   { 
                            url   =   new   URL(destUrl); 
                            httpUrl   =   (HttpURLConnection)   url.openConnection(); 
                            
                            httpUrl.connect(); 
                            
                            bis   =   new   BufferedInputStream(httpUrl.getInputStream()); 
                            
                            fullPath   =   getFullPath(pathName,this.fileFormat);   
                            fos   =   new   FileOutputStream(fullPath);                           while   (   (size   =   bis.read(buf))   !=   -1)   { 
                                    fos.write(buf,   0,   size); 
                            } 
                            fos.flush(); 
                    } 
                    catch(IOException   e)   { 
                            
                    } 
                    catch(ClassCastException   e)   { 
                            
                    } 
                    finally   { 
                            try   { 
                                    fos.close(); 
                                    bis.close(); 
                                    httpUrl.disconnect(); 
                            } 
                            catch(IOException   e)   { 
                                    
                            } 
                            catch(NullPointerException   e)   { 
                                    
                            }                       
                    }                                           
                    return   getImageName(fullPath);   
                }
      

  2.   

    其实只要这一句BufferedImage image = ImageIO.read(url);先前没看api,后悔啊,