日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpURLConnection根据URL下载图片

發布時間:2024/4/15 编程问答 83 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpURLConnection根据URL下载图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

來看下最簡單的根據URL下載圖片,此方法在實際開發中,不建議使用,因為有些圖片是下載不了的,比如百度上的一些圖片,返回http的響應碼是405

[java]?view plaincopy
  • package?cn.ztz.test;??
  • ??
  • import?java.io.BufferedOutputStream;??
  • import?java.io.File;??
  • import?java.io.FileOutputStream;??
  • import?java.io.InputStream;??
  • import?java.io.OutputStream;??
  • import?java.net.HttpURLConnection;??
  • import?java.net.InetAddress;??
  • import?java.net.URL;??
  • ??
  • public?class?HttpDownLoad?{??
  • ????public?static?void?download(String?url,?String?dir,String?fileName)?{??
  • ????????HttpURLConnection?httpURLConnection?=?null;??
  • ????????OutputStream?out?=?null;??
  • ????????InputStream?in?=?null;??
  • ????????try?{??
  • ????????????URL?sendUrl?=?new?URL(url);??
  • ????????????httpURLConnection?=?(HttpURLConnection)?sendUrl.openConnection();??
  • ????????????httpURLConnection.setRequestMethod("POST");??
  • ????????????httpURLConnection.setRequestProperty("contentType",?"utf-8");??
  • ????????????httpURLConnection.setDoOutput(true);???
  • ????????????httpURLConnection.setUseCaches(false);??
  • ????????????httpURLConnection.setConnectTimeout(3000);??
  • ????????????httpURLConnection.setReadTimeout(3000);??
  • ????????????httpURLConnection.setRequestProperty(??
  • ????????????????????"User-agent",InetAddress.getLocalHost().getHostAddress()?+?":"??
  • ????????????????????????????+?System.getProperty("catalina.home"));??
  • ????????????out?=?httpURLConnection.getOutputStream();??
  • ????????????//?清空緩沖區數據??
  • ????????????out.flush();??
  • ????????????//?獲取HTTP狀態碼??
  • ????????????int?httpStatusCode?=?httpURLConnection.getResponseCode();??
  • ????????????if(httpStatusCode!=200){??
  • ????????????????throw?new?RuntimeException("異常");??
  • ????????????}??
  • ????????????in?=?httpURLConnection.getInputStream();??
  • ????????????//?獲取文件長度??
  • ????????????int?len?=?httpURLConnection.getContentLength();??
  • ????????????//?路徑+文件名??
  • ????????????String?pathAndName?=?dir?+?File.separator?+?fileName;??
  • ????????????//?保存文件??
  • ????????????saveFileByByte(in,?pathAndName,?len);??
  • ????????}?catch?(Exception?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}?finally?{??
  • ????????????if?(out?!=?null)?{??
  • ????????????????try?{??
  • ????????????????????out.close();??
  • ????????????????}?catch?(Exception?e)?{??
  • ????????????????????e.printStackTrace();??
  • ????????????????????throw?new?RuntimeException(e.getMessage());??
  • ????????????????}??
  • ????????????}??
  • ????????????if?(in?!=?null)?{??
  • ????????????????try?{??
  • ????????????????????in.close();??
  • ????????????????}?catch?(Exception?e)?{??
  • ????????????????????e.printStackTrace();??
  • ????????????????}??
  • ????????????}??
  • ????????????if?(httpURLConnection?!=?null)?{??
  • ????????????????httpURLConnection.disconnect();??
  • ????????????????httpURLConnection?=?null;??
  • ????????????}??
  • ????????}??
  • ????}??
  • ??
  • ????//寫文件??
  • ????private?static?void?saveFileByByte(InputStream?in,?String?path,?int?len)??
  • ????????????throws?Exception?{??
  • ????????byte[]?byteDatas?=?new?byte[len];??
  • ????????BufferedOutputStream?bw?=?null;??
  • ????????try?{??
  • ????????????//?創建文件對象??
  • ????????????File?f?=?new?File(path);??
  • ????????????//?創建文件路徑??
  • ????????????if?(!f.getParentFile().exists())??
  • ????????????????f.getParentFile().mkdirs();??
  • ????????????//?寫入文件??
  • ????????????bw?=?new?BufferedOutputStream(new?FileOutputStream(path));??
  • ????????????int?bytesRead?=?0;??
  • ????????????while?((bytesRead?=?in.read(byteDatas,?0,?byteDatas.length))?!=?-1)?{??
  • ????????????????bw.write(byteDatas,?0,?bytesRead);??
  • ????????????}??
  • ????????}?catch?(Exception?e)?{??
  • ????????????e.printStackTrace();??
  • ????????????throw?e;??
  • ????????}?finally?{??
  • ????????????try?{??
  • ????????????????if?(bw?!=?null)??
  • ????????????????????bw.close();??
  • ????????????}?catch?(Exception?e)?{??
  • ????????????????throw?e;??
  • ????????????}??
  • ????????}??
  • ????}??
  • }??
  • 總結

    以上是生活随笔為你收集整理的HttpURLConnection根据URL下载图片的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。