简体   繁体   English

Java:确定String是否为URL(没有www或http),获取其屏幕截图

[英]Java : Determine if String is an URL(without www or http), take its screenshot

I am working on a Spring-MVC webapplication in which we are trying to get a screenshot of an URL. 我正在开发一个Spring-MVC Web应用程序,我们正在其中尝试获取URL的屏幕截图。 Currently I am using PhantomJS for that task, but it's too slow(>10seconds). 目前,我正在为该任务使用PhantomJS,但是它太慢了(> 10秒)。 Also, the URL's have to be with http/https and www for detecting that it's an URL. 另外,URL必须带有http / https和www才能检测到它是URL。 As this is a chat application, there can be simple URL's which users add like helloworld.com . 由于这是一个聊天应用程序,因此可以有用户添加的简单URL,如helloworld.com Any help would be nice. 你能帮忙的话,我会很高兴。 Thank you. 谢谢。

Code: 码:

 String[] words = message.split(" ");
                for( String item : words ){
                     boolean val = ResourceUtils.isUrl(item);
                    if(val){
                        urlIdentifier = calcUrl(item);
                        break;
                    }else {
                        System.out.println("Url false is "+item);
                    }
                }

                if(urlIdentifier!=null){
                    replies.setPreviewIdentifier(urlIdentifier);
                    input.put("preview_identifier",urlIdentifier);
                }

Method to calculate screenshot : 计算屏幕截图的方法:

   private String calcUrl(String website){
        try {
            String identifier = String.valueOf(new BigInteger(130, random).toString(32));
            String previewLocation = msg + "chatthumbs/" + identifier ;

            Process proc = Runtime.getRuntime().exec("phantomjs --ssl-protocol=any " +
                    "/home/deploy/phantom/rasterizepdf.js " +" "+website+" " +previewLocation);
            proc.waitFor();
            BufferedImage image = ImageIO.read(new File("/home/akshay/testme.png"));
            if(image!=null){

                if (image.getWidth() > image.getHeight()) {
                    image = Scalr.resize(image, Scalr.Mode.FIT_TO_HEIGHT, 250, 250);
                } else {
                    image = Scalr.resize(image, Scalr.Mode.FIT_TO_WIDTH, 250, 250);
                }
                image = Scalr.crop(image, 250, 250);
                ImageIO.write(image, "png", new File(previewLocation));
            }
            return identifier;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

Any help would be nice. 你能帮忙的话,我会很高兴。 Thank you. 谢谢。

  1. (a) I think the process of taking a screen shot is taking time. (a)我认为进行屏幕截图的过程很耗时间。 Is this code running on the same device as the chat screen? 此代码是否与聊天屏幕在同一设备上运行? Why not use java.awt.Robot to take the screen shot ? 为什么不使用java.awt.Robot进行屏幕截图? or just save the text why do you need a screen shot? 还是只保存文本,为什么需要截屏?

(b) Is the system too busy/ Use on a SSD to see if faster? (b)系统是否太忙/在SSD上使用以查看速度是否更快?

(c) But curious as to the end application, is this part of a web app? (c)但是对于最终应用程序感到好奇,这是Web应用程序的一部分吗? How will your code run on the client systems? 您的代码将如何在客户端系统上运行? Or will you java agent be installed on all user systems that monitors the web page and takes screen shots? 还是将Java代理安装在监视网页并进行屏幕截图的所有用户系统上? Then why use a web page, use a java app to chat, and parse the text as typed. 然后,为什么要使用网页,使用Java应用程序聊天,并解析键入的文本。

  1. Parsing the text. 解析文本。 What if user types/ pastes a long message? 如果用户键入/粘贴长消息怎么办? Are you parsing everything once or on change? 您是解析一次还是更改所有内容? Think of ways to improve that if it seems to be a problem. 如果这似乎是一个问题,请想办法改善它。 Ignore if not the immediate issue now. 现在,如果不是眼前的问题,请忽略。 Also if the msg is too long the parsing can take a lot of time. 另外,如果味精太长,则解析可能会花费大量时间。 maybe process after every key press or change event (if paste) keep local js copy of previous text to get diff? 也许在每次按键或更改事件(如果粘贴)之后进行处理,保留先前文本的本地js副本以获取差异?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM