package com.company;
import org.jsoup.Jsoup;
import java.io.*;
import java.util.Arrays;
public class JsoupDownloadImage {
public static void main(String[] args) {
String url = "https://api.ixiaowai.cn/api/api.php";
byte[] imageByte;
try {
imageByte = Jsoup.connect(url)//返回connection
.followRedirects(true)//是否跟随跳转
.timeout(3000)//超时时间
.maxBodySize(0)//测试得到Jsoup中有1024字节,查询后找到这个Api可以设置访问最大字节 如果设置0为不限制
.ignoreContentType(true)//是否忽略文件类型
.execute()//返回response
.bodyAsBytes();//返回字节
String str = new String(imageByte);//前期测试。使用时可以删除
System.out.println(Arrays.toString(imageByte));
System.out.println(str);
File dir = new File("D:\\图片");
File file = new File(dir, System.currentTimeMillis() + ".jpg");
if (!dir.exists()) {
if (dir.mkdir()) {
System.out.println("目录不存在,创建成功");
} else {
System.out.println("目录创建失败");
}
}
FileOutputStream fileImageOutputStream;
fileImageOutputStream = new FileOutputStream(file, false);//是否追加文件。true为追加。false为覆盖
fileImageOutputStream.write(imageByte, 0, imageByte.length);
System.out.println("成功");
fileImageOutputStream.close();//输出流关闭
} catch (Exception e) {
e.printStackTrace();
}
main(new String[6]);//死循环。错误递归(本身调用本身。无停止条件)
}
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
喜欢就支持以下吧
请登录后发表评论
注册
社交帐号登录