多么美的一张图啊
简述
想搞些好看的图片下来,用作写文章,工具还是选用jsoup,简单易用,但发现个问题,如下图,有的图片只下载一半,刚开始以为下载姿势不对,后来发现是jsoup限制。
解决
- 运行下面代码,输出1048576,约1M,这个是jsoup默认大小。
String url = "https://d3cbihxaqsuq0s.cloudfront.net/images/17935469_xl.jpg";
System.out.println(Jsoup.connect(url).request().maxBodySize());
- jsoup有带个方法设置大小maxBodySize(3000000),可根据自己情况设置
Connection.Response execute = Jsoup.connect(url).ignoreContentType(true).maxBodySize(3000000).ignoreHttpErrors(true).execute();
byte[] bytes = execute.bodyAsBytes();
Map<String, String> headers = execute.headers();
System.out.println(headers.toString());
System.out.println(bytes.length);
IOUtils.write(bytes,new FileOutputStream("../photo/" + 1223 + ".jpg"));
- jsoup下载图片有两种方式,bodyAsBytes(),bodyStream(),文件比较大时选用后者,因为有带缓存。文件读写直接用的commons-io工具类
String url = "https://d3cbihxaqsuq0s.cloudfront.net/images/17935469_xl.jpg";
System.out.println(Jsoup.connect(url).request().maxBodySize());
Long start = System.currentTimeMillis();
byte[] bytes = Jsoup.connect(url).ignoreContentType(true).maxBodySize(3000000).ignoreHttpErrors(true).execute().bodyAsBytes();
IOUtils.write(bytes,new FileOutputStream("../photo/" + 1223 + ".jpg"));
System.out.println(System.currentTimeMillis()-start);
start = System.currentTimeMillis();
BufferedInputStream bufferedInputStream = Jsoup.connect(url).ignoreContentType(true).maxBodySize(3000000).ignoreHttpErrors(true).execute().bodyStream();
IOUtils.copyLarge(bufferedInputStream,new FileOutputStream("../photo/" + 12233 + ".jpg"));
System.out.println(System.currentTimeMillis()-start);
效果
great
网友评论