JAVA复制网络图片到本地

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class CopyURLImg {
    //url 网络图片地址,http开头
    
//outFile 保存地址
    public void copy(URL url, File outFile) throws Exception{
        OutputStream os = new FileOutputStream(outFile);
        InputStream is = url.openStream();
        byte[] buff = new byte[1024];
        while(true) {
            int readed = is.read(buff);
            if(readed == -1) {
                break;
            }
            byte[] temp = new byte[readed];
            System.arraycopy(buff, 0, temp, 0, readed);
            os.write(temp);
        }
        is.close(); 
        os.close();
    }
    
    public static void main(String[] asd) throws Exception{
        CopyURLImg cui = new CopyURLImg();
        String dz = "E:\img\img2.jpg";
        File file = new File(dz);
        cui.copy(new URL("http://e.hiphotos.baidu.com/image/w%3D310/sign=98471f6acebf6c81f7372ae98c3fb1d7/a5c27d1ed21b0ef41f10635cdfc451da81cb3e33.jpg"), file);
    }
}
原文地址:https://www.cnblogs.com/wuxiang/p/4493697.html