0
点赞
收藏
分享

微信扫一扫

HttpClient下载图片

左手梦圆 2023-04-20 阅读 29


评:
需要的包: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(); 

 } 

 } 

}

举报

相关推荐

0 条评论