抓取jsp页面生成图片

该博客介绍了如何在Java中通过Url2Jpg类将网页转化为图片。首先,从PropertiesUtil类获取配置文件中的属性,然后利用JEditorPane加载URL,等待页面加载完成,最后通过Graphics2D将页面渲染到BufferedImage并保存为jpg文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在anction中写的一方法:

public void aaa()throws Exception{

//String phoneIp=PropertiesUtil.getProperty("phoneIp");//phoneIp:在配置中配置的一个IP在地址

//request.getRemoteAddr();//得到当前的IP地址:得到的是IPv4或都是IPv6的


  String url  = this.getUrl();//用onload方法传入当前JSP页面的URL地址

//存放图片在本地

  //String filePath = PropertiesUtil.getProperty("url2jpgfilepath") + "//file.jpg";//设置图片存放位置(url2jpgfilepath:application.properties中配置的图片存放位置)

//存放在服务上


String filePath = request.getRealPath("resource/pic")+File.separator+user.getAdOrgId()+crmSelCheckId+".jpg";  new Url2Jpg(url, new File(filePath)).producePic();

//request.getRealPath("resource/pic"):得到服务上的一个地址

//File.separator:为加载斜线
 }

PropertiesUtil:为PropertiesUtil.java

package com.sodo.core.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;


/**
 * @author yichao
 *
 */
public class PropertiesUtil {

 private static Map<String,String> contains = new HashMap<String,String>();


 /**
  * 取出资源文件中key对应的值
  *
  * @param key
  *            资源文件中的key
  *
  * @return key对应的值 没找到返回null
  */
 public static String getProperty(String key) {
  String val = null;
  try {
   if (StringUtils.isNotEmpty(key)) {
    val = contains.get(key);
    if (StringUtils.isEmpty(val)) {
     fillContain();
     val = contains.get(key);
    }
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  return val;
 }

 /**
  * 从资源文件中读取相应的key对
  *
  * @throws IOException
  */
 private static void fillContain() throws IOException {
  Properties properties = new Properties();
  
  URL url = PropertiesUtil.class.getClassLoader().getResource(
    "application.properties");
  
  File f  = new File(url.getFile()); 
        FileInputStream in = new FileInputStream(f); 
        // 指定读取文件时以UTF-8的格式读取 
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        properties.load(br);

// xch,采用更通用的方式 2011-11-17
//  properties.load(PropertiesUtil.class.getClassLoader().getResource(
//    "application.properties").openStream());

  Enumeration<?> keySet = properties.propertyNames();
  while (keySet.hasMoreElements()) {
   String key = String.valueOf(keySet.nextElement());
   String value = properties.getProperty(key);
   if (StringUtils.isNotEmpty(value)) {
    if (!contains.containsKey(key)) {
     contains.put(key, value);
    } else {
     String existedValue = contains.get(key);
     if (!existedValue.equals(value)) {
      contains.put(key, value);
     }
    }
   }
  }
 }
}

 

Url2Jpg:为Url2Jpg.java

package com.sodo.core.utils;

import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

@SuppressWarnings("serial")
public class Url2Jpg extends JFrame {

 private String url;
 private File file;

 /**
  * 将打开的网址转换成jpg图片
  *
  * 不支持div层网站
  *
  * @param url
  *            网站地址
  * @param file
  *            图片存放地址
  * @throws Exception
  */
 public Url2Jpg(String url, File file) {
  this.url = url;
  this.file = file;
 }

 /**
  * 根据构造函数内容将url抓屏后的页面生成图片放在file路径对应位置
  *
  * @throws Exception
  */
 public String producePic() throws Exception {
  JEditorPane editorPane = new JEditorPane();
  editorPane.setEditable(false);
  editorPane.setPage(url);
  JScrollPane jsp = new JScrollPane(editorPane);
  getContentPane().add(jsp);
  this.setLocation(0, 0);

  Thread.sleep(5 * 1000);

  setSize(10000, 10000);
  pack();
  BufferedImage image = new BufferedImage(editorPane.getWidth(),
    editorPane.getHeight(), BufferedImage.TYPE_USHORT_565_RGB);
  Graphics2D graphics2D = image.createGraphics();
  editorPane.paint(graphics2D);
  ImageIO.write(image, "jpg", file);
  dispose();
  
   

  return file.getName();
 }

}

 

传URL地址:

  $.post(
     "${ctx}/crm/serfReport_aaa.do",//调用以上的代码
     {"url":window.location.href},//window.location.href此为获取当前的URL地址
     function (data)
     {
     },
    "json");}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值