<%@ page contentType="text/html; charset=UTF-8"%>
新闻快讯
。。。。。。
。。。。。。
class org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader work 0 0/1 * * * ?
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.ui.velocity.VelocityEngineUtils;
public class QuartzJob {
VelocityEngine velocityEngine;
private int count;
public void work(){
////////////////////////////////////////////////////////////////////////////
//这里每个人的写法不同,但不管怎么样,目的只有一个:获得想要的数据。我这里是通过post请求返回数据后解析,最后把想要的数据填入下面的HashMap中就ok了;当然你也可以从数据库中获取数据。
// System.out.println("----------开始HTTP连接----------");
// String url="http://XXX.XXX.XXX.XXX";
// WebClient client=new WebClient();
// String content="";
// try {
// content = client.getWebContentByPost(url,"action=login&loginname=13761083826&password=111111");
// content = new String(content.getBytes("iso-8859-1"),"UTF-8");
// System.out.println(content);
// client=null;
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// System.out.println("----------结束HTTP连接----------");
// System.out.println("----------解析xml开始----------");
// parse(content);
// System.out.println("----------解析xml结束----------");
////////////////////////////////////////////////////////////////////////////
Map model=new HashMap();
model.put("填入需要的key1", "填入需要的值1");
model.put("填入需要的key2", "填入需要的值2");
String result = null;
try {
result=VelocityEngineUtils.mergeTemplateIntoString(this.getVelocityEngine(), "vm/baseInfoMiddle.vm", "UTF-8",model);
} catch (VelocityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// System.out.println("result="+result);//打印看看,如果乱码,将baseInfoMiddle.vm文件保存成UTF-8格式
try {
//通常情况下,静态文件都需要生成在tomcat\webapps\yourProject下
// String toPath="D:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\yourProject\\baseInfoMiddle.jsp";
//如果下面的写法,文件将生成在tomcat\bin下
//String toPath="baseInfoMiddle.jsp";
//文件最终的生成地址,可以像下面那样。这样不管tomcat装在哪里!
String toPath="..\\webapps\\yourProject\\baseInfoMiddle.jsp";
PrintWriter writer = new PrintWriter(toPath,"UTF-8");
writer.println(result);
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "[Quartz任务调度生成文件]:"+count++ );
}
public VelocityEngine getVelocityEngine() {
return velocityEngine;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class WebClient {
public WebClient(){
}
public String getWebContentByGet(String urlString, final String charset,
int timeout) throws IOException {
if (urlString == null || urlString.length() == 0) {
return null;
}
urlString = (urlString.startsWith("http://") || urlString
.startsWith("https://")) ? urlString : ("http://" + urlString)
.intern();
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// 增加报头,模拟浏览器,防止屏蔽
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
// 只接受text/html类型,当然也可以接受图片,pdf,*/*任意,就是tomcat/conf/web里面定义那些
conn.setRequestProperty("Accept", "text/html");
conn.setConnectTimeout(timeout);
try {
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input,charset));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\r\n");
}
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
return sb.toString();
}
public String getWebContentByGet(String urlString) throws IOException {
return getWebContentByGet(urlString, "iso-8859-1", 5000);
}
public String getWebContentByPost(String urlString,String data, final String charset,
int timeout)throws IOException{
if (urlString == null || urlString.length() == 0) {
return null;
}
urlString = (urlString.startsWith("http://") || urlString
.startsWith("https://")) ? urlString : ("http://" + urlString).intern();
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置是否向connection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
// Post 请求不能使用缓存
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 增加报头,模拟浏览器,防止屏蔽
connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");
// 只接受text/html类型,当然也可以接受图片,pdf,*/*任意
connection.setRequestProperty("Accept", "text/xml");
connection.setConnectTimeout(timeout);
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
String content = URLEncoder.encode(data, "utf-8");//+URLEncoder.encode("中文 ", "utf-8");
out.writeBytes(content);
out.flush();
out.close();
try {
//必须写在发送数据的后面
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),charset));
String line;
StringBuffer sb=new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\r\n");
}
if (reader != null) {
reader.close();
}
if (connection != null) {
connection.disconnect();
}
return sb.toString();
}
public String getWebContentByPost(String urlString,String data) throws IOException {
return getWebContentByPost(urlString, data,"iso-8859-1", 5000);
}
public static void main(String[] args) throws IOException {
WebClient client=new WebClient();
// String s = client.getWebContentByGet("http://www.baidu.com");
// s = new String(s.getBytes("iso-8859-1"), "gb2312");
String s = client.getWebContentByPost("http://localhost:8080/Lottery/login.portal","action=login&loginname=13761083826&password=111111");
s = new String(s.getBytes("iso-8859-1"), "UTF-8");
System.out.println(s);
}
}
<%@ page contentType="text/html; charset=UTF-8" %>
$key1
$key2