Java 过滤来自 http 响应的图像

标签 java sockets

使用 Socket 我可以向服务器发送 http 请求并获得 html 响应。我的目标是获取每张图片可能是 png, jpeg, gif,或任何其他图像类型。

但是,通过查看不同网站的响应,我注意到有些图像不使用 html 的 <img>标记,而不是可能在 CSS 中。 我怎样才能同时提取 <img>图片和 css 图片(例如 background-image)? 使用正则表达式从 <img> 获取这些图像 url 是否很好? ?

请不要向我介绍像 Apache HttpClient 这样的 http 类。 我的问题不在 http 协议(protocol)上。

最佳答案

要获取所有图片,包括通过 css 加载的图片,或许还有 js,您需要的不仅仅是 html 代码。 您需要理解 html、css 和 js 的代码。 您需要一个完整浏览器。

幸运的是,Java 自带浏览器。 JavaFX WebEngine . 给它一个 url 或 html,它会加载所有内容。 作为WebKit,它知道最新的图像加载技术,例如CSS border-image .

我们只需要一种方法来获取它的图像。 它不提供 media list , 但由于它是纯 Java,我们可以劫持 Java 的 URL 处理程序来拦截它的请求:

import java.io.IOException; import java.net.URL; import java.net.URLConnection; import javafx.application.Application; import javafx.application.Platform; import javafx.concurrent.Worker; import  javafx.scene.Scene; import javafx.scene.web.WebView; import javafx.stage.Stage;

public class NetworkMonitor extends Application {

   private final String url = "http://www.google.com/";

   public static void main( String[] args ) {
      // Override default http/https handler.  Must do once only.
      URL.setURLStreamHandlerFactory( protocol ->
         protocol.equals( "http" ) ? new HttpHandler() :
         protocol.equals( "https" ) ? new HttpsHandler() : null );
      // Launch as JavaFX app.  Required for WebView / WebEngine.
      launch( args );
   }

   @Override public void start(Stage primaryStage) throws Exception {
      // Create webview and listen for ondone
      WebView v = new WebView();
      v.getEngine().getLoadWorker().stateProperty().addListener( ( prop, old, now ) -> {
         if ( now == Worker.State.SUCCEEDED || now == Worker.State.FAILED )
            Platform.exit(); } );
      // Showing GUI is easiest way to make sure ondone will be fired.
      primaryStage.setScene( new Scene( v ) );
      primaryStage.show();
      // Load the target url.
      v.getEngine().load( url );
   }

   // Your IDE should warn you about the sun package.
   private static class HttpHandler extends sun.net.www.protocol.http.Handler {
      @Override protected URLConnection openConnection(URL url) throws IOException {
         System.out.println( url ); // Capture url!
         return super.openConnection( url );
      }
   }

   // If there is no warning, you need to switch to a better IDE!
   private static class HttpsHandler extends sun.net.www.protocol.https.Handler {
      @Override protected URLConnection openConnection(URL url) throws IOException {
         System.out.println( url ); // Capture url!
         return super.openConnection( url );
      }
   }
}

由于您只询问了如何获取 url,这就是代码的作用。 代码可以根据您的需要进行扩展。

例如,两个decorator URLConnection 的对象应该允许您拦截 getInputStream 调用并查询其 header (以确定 MIME 类型)并 fork 流(以保存图像的副本)。

如果这个答案有用,请不要忘记投票!

关于Java 过滤来自 http 响应的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468319/

相关文章:

java - 如何从属性文件配置 Hystrix 注释?

java - Java 套接字编程

java - 使用 SwingWorker java 在线程池中等待任务

c# - 代码在相等比较时返回 false?

android - 是否可以在 Android Webview 中允许 Flash Socket Client?

java - rmi 与 servlet 与套接字

java.lang.VerifyError : Stack map does not match the one at exception handle

java - 不理解缺少返回语句

java - 如何在android后台服务中监听蓝牙套接字?

java - 发送邮件到 Gmail 帐户