java 从Internet下载文件

xiaoxiao2021-02-28  60

import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; public class HttpUtils { public static void main(String[] args) { String fromFile = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502447858&di=d9cd99ad668028d3149cc343b9f64466&imgtype=jpg&er=1&src=http://up.qqjia.com/z/face01/face06/facejunyong/junyong04.jpg"; //String fromFile = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1501853243631&di=d98f0611d9c55513a817de3f90145ffa&imgtype=0&src=http://upload.besoo.com/file/201610/22/0824478945908.jpg"; String toFile = "D:\\test\\11.jpg"; try { URL website = new URL(fromFile); Object obj = website.getContent();                     System.out.println(obj.getClass().getName());  ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(toFile); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); } catch (IOException e) { e.printStackTrace(); } } }
转载请注明原文地址: https://www.6miu.com/read-71593.html

最新回复(0)