HttpClient下载图片实例
生活随笔
收集整理的這篇文章主要介紹了
HttpClient下载图片实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對于HttpClient的介紹和使用可以登錄官網查看?http://hc.apache.org/httpclient-3.x/
準備:將圖片文件夾放置在tomcat的ROOT目錄下,可參考一下路徑
F:\黑馬297\Java開發工具\tomcat\apache-tomcat-7.0.79\webapps\ROOT
之后啟動tomcat
?
public class HttpClientTest {private static String url = "http://localhost:8080/meinv/meinv1.jpg";public static void main(String[] args) throws Exception {// 創建一個java代碼的瀏覽器HttpClient client = new HttpClient();// 創建get請求實例GetMethod method = new GetMethod(url); // 執行請求int responseStatus = client.executeMethod(method);//HttpStatus http狀態代碼的常量if(HttpStatus.SC_OK != responseStatus){throw new RuntimeException("服務器響應錯誤");}// 獲取服務器response的數據byte[] responseBody = method.getResponseBody();File file = new File("meinv1.jpg");FileOutputStream fos = new FileOutputStream(file);fos.write(responseBody);fos.close();// 釋放鏈接method.releaseConnection();}}?執行完之后:
總結
以上是生活随笔為你收集整理的HttpClient下载图片实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【TypeScript 专题】之 Ts
- 下一篇: 啊实打实大师