java获取远程图片,获取不全
答案:4 悬赏:0 手机版
解决时间 2021-11-28 19:54
- 提问者网友:辞取
- 2021-11-27 21:09
java获取远程图片,获取不全
最佳答案
- 五星知识达人网友:神鬼未生
- 2021-11-27 21:58
用这个吧,这个方法是我从apache commons中扒下来的,没有问题的,记得采纳啊
public static void copyFileFromURL(URL source, File destination) throws IOException {
InputStream input = null;
FileOutputStream output = null;
byte[] buffer = new byte[1024];
input = source.openStream();
if (destination.exists()) {
if (destination.isDirectory()) {
throw new IOException("File '" + destination
+ "' exists but is a directory");
}
if (destination.canWrite() == false) {
throw new IOException("File '" + destination
+ "' cannot be written to");
}
} else {
File parent = destination.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent
+ "' could not be created");
}
}
}
output = new FileOutputStream(destination, true);
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
output.close();
input.close();
}
public static void copyFileFromURL(URL source, File destination) throws IOException {
InputStream input = null;
FileOutputStream output = null;
byte[] buffer = new byte[1024];
input = source.openStream();
if (destination.exists()) {
if (destination.isDirectory()) {
throw new IOException("File '" + destination
+ "' exists but is a directory");
}
if (destination.canWrite() == false) {
throw new IOException("File '" + destination
+ "' cannot be written to");
}
} else {
File parent = destination.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent
+ "' could not be created");
}
}
}
output = new FileOutputStream(destination, true);
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
output.close();
input.close();
}
全部回答
- 1楼网友:一把行者刀
- 2021-11-28 01:55
没问题吧 可能是你其他什么框挡住了吧
- 2楼网友:轻熟杀无赦
- 2021-11-28 00:46
close the stream
- 3楼网友:毛毛
- 2021-11-27 23:37
大部分网络抓图都是网页上带的有图片url的那种。高级的网络抓图支持部分Javascript,其实原理和抓取html页面的一样,解析并拼接javascript中的图片地址,然后批量抓取。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯