자바 그림 의 url 주소 에 따라 그림 을 로 컬 로 다운로드 합 니 다.

1521 단어 자바
자바 그림 의 url 주소 에 따라 그림 을 로 컬 로 다운로드 합 니 다.
이미 알 고 있 는 그림 의 url 주 소 는 자바 코드 를 통 해 로 컬 에 다운로드 하여 코드 를 직접 올 립 니 다.
package demo;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadPicFromURL {
    public static void main(String[] args) {
        String url = "http://192.168.1.158/estun_cs/banner_img/head_pic.jpg";
        String path="d:/test/pic.jpg";
        downloadPicture(url,path);
    }
    //  url    
    private static void downloadPicture(String urlList,String path) {
        URL url = null;
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

좋은 웹페이지 즐겨찾기