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);
    }
}

相关文章:

  • 2021-12-13
  • 2022-12-23
  • 2021-11-01
  • 2021-09-08
  • 2021-07-28
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-08
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2022-01-15
相关资源
相似解决方案