Commit db362c14 by 杨林龙

Merge remote-tracking branch 'origin/master'

2 parents 13aee598 53ef86a5
......@@ -33,6 +33,10 @@
<groupId>com.glsx</groupId>
<artifactId>rest-admin-office</artifactId>
</dependency>
<dependency>
<groupId>com.glsx</groupId>
<artifactId>rest-admin-swagger2</artifactId>
</dependency>
<dependency>
<groupId>com.glsx</groupId>
......@@ -52,6 +56,12 @@
<version>1.2.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.github.fanyong920</groupId>
<artifactId>jvppeteer</artifactId>
<version>1.1.5</version>
</dependency>
</dependencies>
<build>
......
package cn.com.glsx.base.config;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
@Configuration
public class BeansConfig {
@Lazy
@Bean
public Browser browser() throws IOException, ExecutionException, InterruptedException {
BrowserFetcher.downloadIfNotExist(null);
ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(true).build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
return browser;
}
}
......@@ -2,6 +2,7 @@ package cn.com.glsx.base.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
......@@ -14,6 +15,12 @@ public class WebConfig implements WebMvcConfigurer {
// private AuthInterceptor authInterceptor;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
//1.加入的顺序就是拦截器执行的顺序,
//2.按顺序执行所有拦截器的preHandle
......
package cn.com.glsx.base.modules.controller;
import cn.com.glsx.admin.common.util.fileupload.FileConstants;
import cn.com.glsx.admin.common.util.fileupload.FileUploadServerData;
import cn.com.glsx.admin.common.util.fileupload.FileUploadUtil;
import com.glsx.plat.common.utils.SnowFlake;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Maps;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.page.ElementHandle;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.ScreenshotOptions;
import com.ruiyun.jvppeteer.options.WaitForSelectorOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.Base64;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping(value = "/api/browser")
public class BrowserController {
@Autowired
private Browser browser;
@GetMapping("/screenshot")
public R<String> screenshot(@RequestParam("url") String url,
String selector,
String filename,
Integer timeout,
Integer quality) throws IOException, InterruptedException {
//设置默认参数
selector = StringUtils.isNotEmpty(selector) ? selector : "selector";
filename = StringUtils.isNotEmpty(filename) ? filename : SnowFlake.nextSerialNumber();
timeout = timeout != null ? timeout : 1000 * 10;
quality = quality != null ? quality : 80;
log.info("screenshot params: url={},selector={},filename={},timeout={},quality={}", url, selector, filename, timeout, quality);
Page page = browser.newPage();
page.setDefaultTimeout(timeout);
page.goTo(url, false);
//根据dom判断是否加载完
WaitForSelectorOptions waitForSelectorOptions = new WaitForSelectorOptions();
waitForSelectorOptions.setTimeout(timeout);
waitForSelectorOptions.setVisible(Boolean.TRUE);
ElementHandle elementHandle = page.waitForSelector(selector, waitForSelectorOptions);
ScreenshotOptions screenshotOptions = new ScreenshotOptions();
screenshotOptions.setType(MediaType.IMAGE_JPEG.getSubtype());
screenshotOptions.setQuality(quality);
//设置截图范围
screenshotOptions.setFullPage(Boolean.TRUE);
//保存到本地
//screenshotOptions.setPath(filename + ".jpg");
String base64Str = page.screenshot(screenshotOptions);
page.close();
byte[] buffer = Base64.getDecoder().decode(base64Str);
FileUploadServerData uploadServerData = FileUploadUtil.uploadToFileSystem(filename, buffer, FileConstants.JPG_TYPE);
Map<String, String> rtnMap = Maps.newHashMap();
rtnMap.put("url", uploadServerData.getSecureUrl());
rtnMap.put("base64", base64Str);
return R.ok().data(rtnMap);
}
}
package cn.com.glsx.test;
import com.glsx.plat.common.utils.SnowFlake;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.ElementHandle;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.options.ScreenshotOptions;
import com.ruiyun.jvppeteer.options.WaitForSelectorOptions;
import org.apache.commons.io.FileUtils;
import org.springframework.http.MediaType;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
public class SnapshotTest {
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
//方案一
puppeteer();
//方案二
// screenshotmaster();
}
public static void puppeteer() throws IOException, ExecutionException, InterruptedException {
BrowserFetcher.downloadIfNotExist(null);
ArrayList<String> arrayList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(true).build();
arrayList.add("--no-sandbox");
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Page page = browser.newPage();
page.setDefaultTimeout(1000 * 6);
// page.setViewport(new Viewport(1024, 768, 1, false, false, false));
page.goTo("https://map.baidu.com", false);
// 根据dom判断是否加载完
WaitForSelectorOptions waitForSelectorOptions = new WaitForSelectorOptions();
waitForSelectorOptions.setTimeout(1000 * 15);
waitForSelectorOptions.setVisible(Boolean.TRUE);
ElementHandle elementHandle = page.waitForSelector("testdom", waitForSelectorOptions);
ScreenshotOptions screenshotOptions = new ScreenshotOptions();
screenshotOptions.setType(MediaType.IMAGE_JPEG.getSubtype());
screenshotOptions.setQuality(80);
//设置截图范围FullPage、Clip二选一
screenshotOptions.setFullPage(Boolean.TRUE);
// Clip clip = new Clip(1.0, 1.56, 400, 400);
// screenshotOptions.setClip(clip);
//设置存放的路径
String filename = SnowFlake.nextSerialNumber();
screenshotOptions.setPath("E:\\" + filename + ".jpg");
String base64Str = page.screenshot(screenshotOptions);
System.out.println(filename + "====" + base64Str);
page.close();
}
public static void screenshotmaster() {
try {
//参数
String token = "62cf8d72c0035";
String url = URLEncoder.encode("https://www.ithome.com");
int width = 1280;
int height = 800;
int full_page = 1;
// 构造URL
String query = "https://www.screenshotmaster.com/api/v1/screenshot";
query += String.format("?token=%s&url=%s&width=%d&height=%d&full_page=%s",
token, url, width, height, full_page);
URL apiUrl = new URL(query);
// 调用API并将结果保存进screenshot.png
InputStream inputStream = apiUrl.openStream();
FileUtils.copyToFile(inputStream, new File("E:\\screenshot.png"));
inputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!