import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
public class TestTime {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
String url = null;
url="http://www.baidu.com/img/baidu_sylogo1.gif"; //可以通过爬虫得到图片连接
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
httpClient.executeMethod(getMethod); //执行getMrthod
byte[] responseBody = getMethod.getResponseBody();
OutputStream out = new FileOutputStream(new File("D:\\"+1+".jpg"));
out.write(responseBody);
out.flush();
System.out.println("download success");
out.close();
}
}
该Java代码示例展示了如何从指定URL下载图片,将其转换为byte数组,并保存到本地为jpg文件。使用了HttpClient库执行HTTP GET请求获取图片数据,然后通过FileOutputStream写入文件。
875

被折叠的 条评论
为什么被折叠?



