在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");}