提交修改

This commit is contained in:
quyixiao 2020-12-24 17:25:22 +08:00
parent 5f333863b9
commit d8c306a651
4 changed files with 257 additions and 0 deletions

62
pom.xml
View File

@ -267,6 +267,68 @@
<version>5.1.3.201810200350-r</version>
</dependency>
<dependency>
<groupId>com.lz.crawler</groupId>
<artifactId>crawler-tools</artifactId>
<version>2.9-SNAPSHOT</version>
</dependency>
<!-- seleniumhq -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.52.0</version>
</dependency>
<!-- guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,91 @@
package com.lz.common.utils;
import com.admin.crawler.engine.utils.ToolsOrderUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.concurrent.TimeUnit;
@Slf4j
public class CutLongImageUtils {
public static String getLongImageUrl(String httpUrl) {
String url = null;
//设置必要参数
DesiredCapabilities dcaps = new DesiredCapabilities();
//ssl证书支持
dcaps.setCapability("acceptSslCerts", true);
//截屏支持
dcaps.setCapability("takesScreenshot", true);
//css搜索支持
dcaps.setCapability("cssSelectorsEnabled", true);
//js支持
dcaps.setJavascriptEnabled(true);
if(OSUtils.isMacOSX()){
//驱动支持第二参数表明的是你的phantomjs引擎所在的路径使用whereis phantomjs可以查看
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/usr/local/bin/phantomjs");
}else if(OSUtils.isWindows()){
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "xxxxxxxxx");
}else if(OSUtils.isLinux()){
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/usr/bin/phantomjs");
}
//驱动支持
//创建无界面浏览器对象
PhantomJSDriver driver = new PhantomJSDriver(dcaps);
String fileName1 = null;
try {
// 让浏览器访问空间主页
//设置隐性等待作用于全局
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.get(httpUrl);
Long width = (Long) driver.executeScript("return document.body.parentNode.scrollWidth");
Long height = (Long) driver.executeScript("return document.body.parentNode.scrollHeight");
System.out.println("width=" + width + ",height=" + height);
Dimension dimension = new Dimension(width.intValue(), height.intValue());
driver.manage().window().setSize(dimension);
String userHome = System.getProperty("user.home");
if (!userHome.endsWith("/")) {
userHome = userHome + "/";
}
userHome = userHome + "captcha/";
fileName1 = userHome + ToolsOrderUtil.getUserPoolOrder("pic") + "screenshot1.png";
log.info("文件名 = " + fileName1);
File screenshot = (File) ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
ImageIO.write(fullImg, "png", screenshot);
File screenshotLocation = new File(fileName1);
FileUtils.copyFile(screenshot, screenshotLocation);
url = OSSUtils.uploadImage(fileName1);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭并退出浏览器
System.out.println("=============================");
driver.close();
driver.quit();
File file = new File(fileName1);
if (file.exists()) {
file.delete();
}
}
return url;
}
public static void main(String[] args) {
System.out.println(getLongImageUrl("https://www.csdn.net/"));
}
}

View File

@ -0,0 +1,95 @@
package com.lz.common.utils;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* 操作系统类
* 获取System.getProperty("os.name")对应的操作系统
*
* @author isea533
*/
public class OSUtils {
private static final boolean osIsMacOsX;
private static final boolean osIsWindows;
private static final boolean osIsWindowsXP;
private static final boolean osIsWindows2003;
private static final boolean osIsWindowsVista;
private static final boolean osIsLinux;
private static final boolean osIsWindowsWin7;
private static final boolean osIsWindowsWin8;
private static final boolean osIsWindowsWin10;
static {
String os = System.getProperty("os.name");
if (os != null)
os = os.toLowerCase();
osIsMacOsX = "mac os x".equals(os);
osIsWindows = os != null && os.indexOf("windows") != -1;
osIsWindowsXP = "windows xp".equals(os);
osIsWindows2003 = "windows 2003".equals(os);
osIsWindowsVista = "windows vista".equals(os);
osIsLinux = os != null && os.indexOf("linux") != -1;
osIsWindowsWin7 = os != null && os.indexOf("windows 7") != -1;
osIsWindowsWin8 = os != null && os.indexOf("windows 8") != -1;
osIsWindowsWin10 = os != null && os.indexOf("windows 10") != -1;
}
public static boolean isMacOSX() {
return osIsMacOsX;
}
public static boolean isWindows() {
return osIsWindows;
}
public static boolean isWindowsXP() {
return osIsWindowsXP;
}
public static boolean isWindows2003() {
return osIsWindows2003;
}
public static boolean isWindowsVista() {
return osIsWindowsVista;
}
public static boolean isLinux() {
return osIsLinux;
}
public static boolean IsWindowsWin7() {
return osIsWindowsWin7;
}
public static boolean IsWindowsWin8() {
return osIsWindowsWin8;
}
public static boolean IsWindowsWin10() {
return osIsWindowsWin10;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(OSUtils.isMacOSX());
}
public static void sleepMilliSecond(int min, int max) {
try {
Random random = new Random();
int time = random.nextInt(max) % (max - min + 1) + min;
TimeUnit.MILLISECONDS.sleep(time);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dingtalk.api.response.OapiCrmAuthGroupMemberListResponse;
import com.lz.common.utils.CutLongImageUtils;
import com.lz.common.utils.R;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.dao.DepartmentsDao;
@ -330,4 +331,12 @@ public class TestController {
public R commentList(TaskModel taskModel) {
return staffRoleService.commentList(taskModel);
}
//http://localhost:8080/lz_management/test/cutimage
@RequestMapping("/test/cutimage")
public R cutimage() {
String url = CutLongImageUtils.getLongImageUrl("https://www.baidu.com/");
return R.ok().put("url",url);
}
}