java下载图片到本地实例

阅读更多
public static void main(String[] args) { 
        String ImageUrl = "xxxxxxxxx";//图片url 
        String path="F://createVerifyCode.jpg";//  保存地址
        downloadPic(ImageUrl ,path); 
    } 



    //链接url下载图片 
    public static boolean downloadPic(String urlList,String path) { 
        URL url = null; 
        try { 
            url = new URL(urlList); 
            DataInputStream dataInputStream = new DataInputStream(url.openStream()); 
 
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); 
            ByteArrayOutputStream output = new ByteArrayOutputStream(); 
 
            byte[] buffer = new byte[1024]; 
            int length; 
 
            while ((length = dataInputStream.read(buffer)) > 0) { 
                output.write(buffer, 0, length); 
            } 
            fileOutputStream.write(output.toByteArray()); 
            dataInputStream.close(); 
            fileOutputStream.close(); 
            return true;
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return false;
    } 

你可能感兴趣的:(java)