HttpClient下载图片

源:http://hbluojiahui.blog.163.com/blog/static/3106476720093753244765/
评:
需要的包:commons-httpclient.jar,commons-loggin.jar,commons-codec-1.3.jar

package com.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

public class DownloadImage {
    public static void main(String args[]){
        new DownloadImage().download("http://bbs.sh133.cn/attachments/month_0606/15_ESOb64WJNCyP.jpg");
    }

    //url为图片地址
    public void download(String url)
    {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(url);
        try {
        client.executeMethod(get);
        String name = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        File storeFile = new File("C:/"+name + ".jpg");
        FileOutputStream fileOutputStream = new FileOutputStream(storeFile);
        FileOutputStream output = fileOutputStream;
        output.write(get.getResponseBody());
        output.close();
        } catch (HttpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
}

你可能感兴趣的:(httpclient)