[英]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.
谢谢。
(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应用程序聊天,并解析键入的文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.