Java实现URL下载图片到本地
生活随笔
收集整理的這篇文章主要介紹了
Java实现URL下载图片到本地
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
功能:輸入圖片URL地址和圖片名字,輸出下載圖片到指定目錄。參考代碼如下:
import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL;public static void downloadImage(String Imageurl,String filename) throws IOException{System.getProperties().setProperty("http.proxyHost", "IP");//設置代理System.getProperties().setProperty("http.proxyPort", "Port");URL url = new URL(Imageurl);//打開網絡輸入流DataInputStream dis = new DataInputStream(url.openStream());String newImageName="D://tmp//"+filename+".jpg";//建立一個新的文件FileOutputStream fos = new FileOutputStream(new File(newImageName));byte[] buffer = new byte[1024];int length;//開始填充數據while((length = dis.read(buffer))>0){fos.write(buffer,0,length);}dis.close();fos.close(); }總結
以上是生活随笔為你收集整理的Java实现URL下载图片到本地的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模拟浏览器自动化测试工具Selenium
- 下一篇: MapReduce基础开发之十三File