当前位置: 代码迷 >> J2EE >> 图片读取解决方案
  详细解决方案

图片读取解决方案

热度:148   发布时间:2016-04-22 02:32:35.0
图片读取
怎么根据一个url读取图片,然后成为一个BufferedImage对象
比如这个是读取文件的BufferedImage image = ImageIO.read(imageFile);
那么怎么读取url,请指点一下,谢谢

------解决方案--------------------
Java code
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);               }
  相关解决方案