com.safframework.tony.common.utils.IOUtils Java Examples

The following examples show how to use com.safframework.tony.common.utils.IOUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SeleniumUtils.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
public static void taskScreenShot(WebDriver driver,WebElement element,String pathName) {

        //指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        //利用IOUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。

        try {
            //获取元素在所处frame中位置对象
            Point p = element.getLocation();
            //获取元素的宽与高
            int width = element.getSize().getWidth();
            int height = element.getSize().getHeight();
            //矩形图像对象
            Rectangle rect = new Rectangle(width, height);
            BufferedImage img = ImageIO.read(srcFile);
            BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
            ImageIO.write(dest, "png", srcFile);
            IOUtils.copyFile(srcFile, new File(pathName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
Example #2
Source File: SeleniumUtils.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
/**
 * 截取某个区域的截图
 * @param driver
 * @param x
 * @param y
 * @param width
 * @param height
 * @param pathName
 */
public static void taskScreenShot(WebDriver driver,int x,int y,int width,int height,String pathName) {

    //指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
    File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    //利用IOUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。

    try {
        //矩形图像对象
        Rectangle rect = new Rectangle(width, height);
        BufferedImage img = ImageIO.read(srcFile);
        BufferedImage dest = img.getSubimage(x, y, rect.width, rect.height);
        ImageIO.write(dest, "png", srcFile);
        IOUtils.copyFile(srcFile, new File(pathName));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: SpiderUtils.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
/**
 * 导出csv
 *
 * @param file
 * @param dataList
 * @param charset
 * @return
 */
public static boolean exportCsv(File file, List<String> dataList, Charset charset) {

    boolean isSucess = false;

    FileOutputStream out = null;
    OutputStreamWriter osw = null;
    BufferedWriter bw = null;
    try {
        out = new FileOutputStream(file);
        osw = new OutputStreamWriter(out, charset);
        bw = new BufferedWriter(osw);
        if (Preconditions.isNotBlank(dataList)) {
            for (String data : dataList) {
                bw.append(data).append("\r");
            }
        }
        isSucess = true;
    } catch (Exception e) {
        isSucess = false;
    } finally {
        IOUtils.closeQuietly(bw,osw,out);
    }

    return isSucess;
}
 
Example #4
Source File: FileDownloadAfterRequest.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
@Override
public void process(Page page) {

    InputStream is = page.getResultItems().get(Constant.RESPONSE_RAW);

    if (is!=null) {

        try {
            // 创建保存文件的目录
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }
            // 创建保存的文件
            File file = new File(filePath + File.separator + fileName);

            IOUtils.writeToFile(is,file);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}
 
Example #5
Source File: Utils.java    From PicCrawler with Apache License 2.0 6 votes vote down vote up
/**
 * 对当前对url进行截屏,一方面可以做调试使用看能否进入到该页面,另一方面截屏的图片未来可以做ocr使用
 * @param url
 */
public static void getScreenshot(String url) {

    //启动chrome实例
    WebDriver driver = new ChromeDriver();
    driver.get(url);
    //指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
    File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    //利用IOUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。

    try {
        IOUtils.copyFile(srcFile, new File("screenshot.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    //关闭浏览器
    driver.quit();
}
 
Example #6
Source File: Utils.java    From PicCrawler with Apache License 2.0 6 votes vote down vote up
/**
 * 对当前对url进行截屏,一方面可以做调试使用看能否进入到该页面,另一方面截屏的图片未来可以做ocr使用
 * @param url
 * @param pathName
 */
public static void getScreenshot(String url,String pathName) {

    //启动chrome实例
    WebDriver driver = new ChromeDriver();
    driver.get(url);
    //指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
    File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    //利用IOUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。

    try {
        IOUtils.copyFile(srcFile, new File(pathName));
    } catch (IOException e) {
        e.printStackTrace();
    }

    //关闭浏览器
    driver.quit();
}
 
Example #7
Source File: SeleniumUtils.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
public static void taskScreenShot(WebDriver driver,String pathName){

        //指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        //利用IOUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。

        try {
            IOUtils.copyFile(srcFile, new File(pathName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
Example #8
Source File: HttpManager.java    From ProxyPool with Apache License 2.0 5 votes vote down vote up
public Page getWebPage(String url, String charset ,Proxy proxy) throws IOException {

        Page page = new Page();

        CloseableHttpResponse response = null;

        if (proxy == null) {

            response = HttpManager.get().getResponse(url);
        } else {

            response = HttpManager.get().getResponse(url,proxy);
        }

        if (response!=null) {
            page.setStatusCode(response.getStatusLine().getStatusCode());
            page.setUrl(url);
            try {
                if(page.getStatusCode() == 200){

                    page.setHtml(EntityUtils.toString(response.getEntity(), charset));
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

                IOUtils.closeQuietly(response);
            }
        } else { // response is null

            page.setUrl(url);
        }

        return page;
    }
 
Example #9
Source File: Spider.java    From NetDiscovery with Apache License 2.0 2 votes vote down vote up
private void stopSpider(Downloader downloader) {

        IOUtils.closeQuietly(downloader);

        stop();
    }