java">import org. jsoup. Connection ;
import org. jsoup. Jsoup ;
import org. jsoup. nodes. Document ;
import org. jsoup. nodes. Element ;
import org. jsoup. select. Elements ;
import java . io. FileOutputStream ;
public class ImageDownloader {
public static void main ( String [ ] args) throws Exception {
String url = "https://www.xx.com/hot" ;
Connection connection = Jsoup . connect ( url) ;
Document document = connection. get ( ) ;
Elements images = document. getElementsByTag ( "img" ) ;
for ( Element image : images) {
String srcUrl = image. attr ( "src" ) ;
if ( ! srcUrl. isEmpty ( ) ) {
downloadImage ( srcUrl) ;
}
}
}
private static void downloadImage ( String url) throws Exception {
Connection connection = Jsoup . connect ( url) . ignoreContentType ( true ) ;
byte [ ] bytes = connection. execute ( ) . bodyAsBytes ( ) ;
int lastIndexOfSlash = url. lastIndexOf ( "/" ) + 1 ;
String fileName = url. substring ( lastIndexOfSlash) ;
FileOutputStream outputStream = new FileOutputStream ( "E:\\JsoupPic\\" + fileName) ;
outputStream. write ( bytes) ;
outputStream. close ( ) ;
System . out. println ( "已下载图片:" + fileName) ;
}
}