利用HttpClient 下载图片到本地

public static void main(String[] args) throws IOException
    {
        try
        {
            // hasActive();
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod("http://a.jpg");
            int i = client.executeMethod(get);
            if (200 == i)
            {
                File storeFile = new File("c:/bb.gif");
                FileOutputStream output = new FileOutputStream(storeFile);
                // 得到网络资源的字节数组,并写入文件
                output.write(get.getResponseBody());
                output.close();
            }else
            {
                System.out.println("no pic"); 
            }
        } catch ( Exception e )
        {
            System.out.println("no pic");
        }

    }

相关推荐