java 模拟浏览器实现导入

package com.paic.opencms;


import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.junit.Test;


public class ImportFromExcel {

private static final Log logger = LogFactory.getLog(ImportFromExcel.class);

private static final String CMS_HOST = "http://pa18-wcm-stg.paic.com.cn";
private static final String CMS_USER = "EX-HETAIHE001";
private static final String CMS_PASSWORD = "aaaaa888";
private static final String CURRENT_FOULDER = "%2Fcaozuozhinan%2Fcontent%2F";
private static final String CURRENT_FOULDER_PATH = "/caozuozhinan/content/";
private static final String CURRENT_TYPE = "xitongshouce";
private static final String CURRENT_TYPE_ZI = null;
private static final String XSD_TYPE = "ex_jiaoyizhinan";

private DefaultHttpClient client = null;

private  int count;
private  int indexcount=60;

private HashMap fileMap= new HashMap();
private HashMap reMap= new HashMap();
@Test
public void initFileMap(){
try {
String filepath="D:\\jpgs";
File file=new File(filepath);
String[] fileList = file.list();
for (int i = 0; i < fileList.length; i++) {
String[] filename = fileList[i].split("\\.");
        fileMap.put(filename[0], "http://resources.pingan.com/app_upload/images/ex/contentimg/"+fileList[i])   ;  

}

} catch (Exception e) {
// TODO: handle exception
}
}
@Test
public void init(){
logger.debug("initFileMap...............");
initFileMap();
logger.info("启动从excel导入opencms程序……:" +fileMap.size());
logger.info("-----创建虚拟浏览器……");
client = new DefaultHttpClient();
client.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 5000);
//登陆opencms
boolean login = this.loginOpenCms();
if(login){
//登陆成功
try {


logger.debug("执行工作区跳转……");
HttpResponse resp = this.getGet(CMS_HOST + "/opencms/opencms/system/workplace/views/workplace.jsp");
String entity = EntityUtils.toString(resp.getEntity());
logger.debug(entity);

logger.debug("执行站点切换……");
Map switchSiteParams = new HashMap();
switchSiteParams.put("wpFrame", "head");
switchSiteParams.put("wpSite", "/sites/ex.pingan.com");
resp = getPost(CMS_HOST + "/opencms/opencms/system/workplace/views/top_head.jsp", switchSiteParams);
entity = EntityUtils.toString(resp.getEntity());
logger.debug(entity);

logger.debug("执行文章导入……");
importArticle();
logger.debug("文章导入执行结束……");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
//登陆失败 退出程序
}
}

/**
* 导入文章
*/
public void importArticle(){
Map params = new HashMap();
Workbook book = null;
try {
book = Workbook.getWorkbook(new File("D:/info/4_1.xls"));
Sheet sheet = book.getSheet(0);
int articleCount = sheet.getRows();
for(int i=0; i //清空 替换map
reMap.clear();
logger.info("------------------------------------->创建文章"+i);
logger.debug("------------------------------------->创建文章");
String fileName = createArticle();
params = xlsMapping(params, sheet, i);
logger.debug("------------------------------------->打开编辑文章");
fileName = openEdit(fileName);
logger.debug("------------------------------------->保存编辑文章");
saveEdit(fileName, params);
logger.debug("------------------------------------->退出编辑文章");
exitEdit(fileName);
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(book != null){
book.close();
}
}

}

public Map xlsMapping(Map params, Sheet sheet, int rows){
/* String date = sheet.getCell(0 , rows).getContents();
logger.debug("==========date:" + date);
String title = sheet.getCell(1, rows).getContents();
title = (title != null ? title.trim() : title);
logger.info("==========title:" + title);
String content = sheet.getCell(2, rows).getContents();
// String content_encode = (content != null ? URLEncoder.encode(content) : content );
// content_encode = (content_encode != null ? content_encode.trim() : content_encode );
// logger.info("==========content_encode:" + content_encode);
logger.info("==========content:" + content);
String source = sheet.getCell(3, rows).getContents();
source = (source != null ? source.trim() : source);
logger.info("==========source:" + source);
String type = sheet.getCell(4, rows).getContents();

logger.info("==========type:" + type);
String keywords = sheet.getCell(5, rows).getContents();
logger.info("==========keywords:" + keywords);
String description = sheet.getCell(6, rows).getContents();
logger.info("==========description:" + description);
*/
// String date = "2013-06-27";
// String title = "解码6月抗跌基金  低仓位+新兴产业";
// String content = "解码6月抗跌基金  低仓位+新兴产业";
// String source = "上海证券报";
// String type = "基金要闻";
// String keywords = "基金要闻、基金评述、基金看市、基金资讯、平安银行";
// String description = "提供基金要闻、基金评述、基金看市等相关基金资讯";

String title = sheet.getCell(0, rows).getContents();
title = (title != null ? title.trim() : title);


logger.info("==========title:" + title);
String content = sheet.getCell(1, rows).getContents();
// String content_encode = (content != null ? URLEncoder.encode(content) : content );
// content_encode = (content_encode != null ? content_encode.trim() : content_encode );
// logger.info("==========content_encode:" + content_encode);
//logger.info("==========content:" + content);
String source = sheet.getCell(2, rows).getContents();
source = (source != null ? source.trim() : source);

logger.info("==========source:" + source);
String date = sheet.getCell(3, rows).getContents();
logger.info("==========date:" + date);

String type = CURRENT_TYPE;

logger.info("==========type:" + type);



/*String contentFormat = content.replaceAll(" ", " ");
contentFormat = content.replaceAll("[\\t\\n\\r]", "
");
contentFormat = contentFormat.trim();*/

String desc = content.trim();
desc = (desc.length() > 150 ? desc.substring(0, 150) : desc);


/* params.put("OpenCmsDateTime.basicInfo_cn_1_.date_1_.0", date);
params.put("OpenCmsString.basicInfo_cn_1_.desc_1_.0", desc);
params.put("OpenCmsString.basicInfo_cn_1_.directory_1_.0", type);
params.put("OpenCmsString.basicInfo_cn_1_.source_1_.0", source);
params.put("OpenCmsString.basicInfo_cn_1_.title_1_.0", title);
params.put("ta_OpenCmsHtml.page_cn_1_.contentDetail_1_.0", content);
params.put("OpenCmsHtml.page_cn_1_.contentDetail_1_.0", URLEncoder.encode(contentFormat));*/

//特殊处理----begin
if(content.indexOf("http://10.37.160.38:8080")>0){
content.replaceAll("http://10.37.160.38:8080", "");
}
count= content.indexOf("/manage/infomanage/servlet/download?type=info&attachmentID=");
if(count<0){
count= content.indexOf("/manage/infomanage/servlet/download?type=info&attachmentID=");
if(count>=0){
indexcount=64;
}else{
indexcount=60;
}
}else{
indexcount=60;
}
System.out.println(count);
while (count>=0) {
getInt(count,content);
}
String contentNew=getReplaceAll(content);
logger.info("==========contentNew:" + contentNew);
//特殊处理----end
params.put("OpenCmsDateTime.publishDate_1_.0", date+" PM 2:52");
params.put("OpenCmsHtml.page_cn_1_.contentDetail_1_.0", URLEncoder.encode(contentNew));
params.put("OpenCmsString.source_1_.0", source);
params.put("OpenCmsString.exleixing_1_.0", type);
if(CURRENT_TYPE_ZI!=null){
params.put("OpenCmsString.ziexleixing_1_.0", CURRENT_TYPE_ZI);
}
params.put("OpenCmsBoolean.iszuixin_1_.0", "0");
params.put("OpenCmsString.title_1_.0", title);
return params;
}

/**
* 创建Article
* @return
*/
public String createArticle(){
//取当前时间戳为文件名
String fileName = System.currentTimeMillis() + ".shtml";
Map params = new HashMap();
params.put("action", "checkmodel");
params.put("currentfolder", CURRENT_FOULDER);
params.put("dialogtype", "newresource");
params.put("formname", "nrt-form");
params.put("framename", "explorer_files");
params.put("newformuri", "newresource.jsp%3Fpage%3Dstructurecontent");
params.put("newresourcetype", XSD_TYPE);
params.put("newresourceuri", "newresource_xmlcontent.jsp%3Fnewresourcetype%3D"+XSD_TYPE);
params.put("next", "继续 >>");
params.put("resource", fileName);
params.put("style", "");
params.put("title", "%E5%88%9B%E5%BB%BA%E6%96%B0%E6%96%87%E4%BB%B6");

try {
HttpResponse resp = getPost(CMS_HOST + "/opencms/opencms/system/workplace/commons/newresource_xmlcontent.jsp", params);
String entity = EntityUtils.toString(resp.getEntity());
logger.debug(entity);

//TODO 增加创建成功与否判断
return fileName;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

}

/**
* 打开要编辑的Article
* @param fileName
* @return
*/
public String openEdit(String fileName){


String url = CMS_HOST + "/opencms/opencms/system/workplace/editors/xmlcontent/editor_form.jsp?resourcelist=&resource=" + CURRENT_FOULDER + fileName;
try {
HttpResponse resp = this.getGet(url);
String entity = EntityUtils.toString(resp.getEntity());
logger.debug(entity);
//TODO 增加打开编辑成功与否判断
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return fileName;
}

/**
* 保存编辑
* @param fileName
* @return
*/
public String saveEdit(String fileName, Map params){
String url = CMS_HOST + "/opencms/opencms/system/workplace/editors/xmlcontent/editor_form.jsp";
params.put("action", "saveexit");
params.put("backlink", "");
params.put("choiceelement", "");
params.put("choicetype", "");
params.put("directedit", "null");
params.put("editastext", "");
params.put("elementindex", "");
params.put("elementlanguage", "zh");
params.put("elementname", "");
params.put("loaddefault", "null");
params.put("modified", "true");
params.put("oldelementlanguage", "zh");
params.put("tempfile", CURRENT_FOULDER_PATH + "~" + fileName);
params.put("resource", CURRENT_FOULDER_PATH + fileName);
try {
HttpResponse resp = getPost(url, params);
String entity = EntityUtils.toString(resp.getEntity());
logger.info(entity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileName;
}

/**
* 退出编辑
* @param fileName
* @return
*/
public String exitEdit(String fileName){
String url = CMS_HOST + "/opencms/opencms/system/workplace/editors/xmlcontent/editor_form.jsp";
Map params = new HashMap();
params.put("action", "exit");
params.put("backlink", "");
params.put("directedit", "null");
params.put("elementlanguage", "zh");
params.put("modified", "true");
params.put("resource", CURRENT_FOULDER_PATH + fileName);
params.put("tempfile", CURRENT_FOULDER_PATH + "~" + fileName);

try {
HttpResponse resp = getPost(url, params);
String entity = EntityUtils.toString(resp.getEntity());
logger.debug(entity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileName;
}

/**
* 登陆opencms方法
* @return
*/
public boolean loginOpenCms(){
logger.debug("init login method...");
boolean flag = false;
Map params = new HashMap();
params.put("login", "true");
params.put("ocOuSearch", "Search organizational unit");
params.put("ocOuFqn", "/");
params.put("ocPword", CMS_PASSWORD);
params.put("ocUname", CMS_USER);
params.put("requestedResource", "EX-%2Fsystem%2Fworkplace%2Fviews%2Fworkplace.jsp");
try {
HttpResponse resp = this.getPost(CMS_HOST + "/opencms/opencms/system/login/index.html", params);
String entity = EntityUtils.toString(resp.getEntity());
logger.debug(entity);
} catch (Exception e) {
logger.error("登陆链接异常……");
flag = false;
}

//增加登陆判断逻辑
flag = true;

logger.debug("exit login method...");
return flag;
}


/**
* 创建get请求
* @param url
* @param params
* @return
* @throws Exception
*/
public HttpResponse getGet(String url, Map params) throws Exception {
logger.debug("init create get request method2……");
try {


StringBuilder paramsBuilder = new StringBuilder("?");
Set> set = params.entrySet();
Iterator> iter = set.iterator();
while(iter.hasNext()){
Entry entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
paramsBuilder.append(key);
paramsBuilder.append("=");
paramsBuilder.append(value);
paramsBuilder.append("&");
}

paramsBuilder.deleteCharAt(paramsBuilder.length());
url = url + paramsBuilder.toString();

HttpGet getMethod = new HttpGet(url);
HttpResponse response = client.execute(getMethod);


logger.debug("eixt create get request method1……");
return response;
} catch (Exception e) {
logger.error("创建get请求异常……", e);
throw new Exception("创建get请求异常……", e);
}
}

/**
* 创建get请求
* @param url
* @return
* @throws Exception
*/
public HttpResponse getGet(String url) throws Exception{
logger.debug("init create get request method1……");

try {

HttpGet getMethod = new HttpGet(url);
HttpResponse response = client.execute(getMethod);

logger.debug("eixt create get request method1……");
return response;
} catch (Exception e) {
logger.error("创建get请求异常……", e);
throw new Exception("创建get请求异常……", e);


}

/**
* 创建post请求方法
* @param url
* @param params
* @return
* @throws Exception
*/
public HttpResponse getPost(String url,Map params) throws Exception{

logger.debug("init create post request method……");


try {

List nvps = new ArrayList();

Set> set = params.entrySet();
Iterator> iter = set.iterator();
while(iter.hasNext()){
Entry entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
nvps.add(new BasicNameValuePair(key, value));
}

HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = client.execute(post);

logger.debug("exit create post request method……");
return response;
} catch (UnsupportedEncodingException e) {

logger.error("添加post参数异常……", e);
throw new Exception("添加post参数异常……", e);
} catch(Exception e){

logger.error("创建Post请求异常……", e);
throw new Exception("创建Post请求异常……", e);
}

}

public static void main(String[] args) {
String a ="

  4月14日,随着铿锵有力的铓锣声、鼓点声,云南省西双版纳傣族自治州10580名身着绚烂服饰的群众演员,在景洪市泼水广场上舞动花伞,翩翩起舞。转动的花伞,像一片片飘动的彩云、一团团燃烧的彩霞、一朵朵芬芳的缅桂,更像粉红荷花瓣撒落傣乡大地,使美丽西双版纳更加风情万种,令世界瞩目。

"+
"

"+
"

  在此间花车大巡游也是广大民众最关注的一项饕餮盛宴, 12辆缤纷亮丽的花车悉数登场,期间穿插着各式各样名族特色的精彩表演,当天吸引了数万的民众围绕在景洪城澜沧江畔边,共同感受傣历1376年新年节(泼水节)的民族韵味。

"+
"

"+
"

  今年平安交易所也参与了此次花车大巡游活动,当代表交易所的“金色勐旺”横空出现,立刻博得了现场观众的一致的喝彩。平安交易所为版纳经济发展添力,利用彩车巡游这一平台,来广泛传播交易所品牌正能量,交易所为版纳当地人民平安幸福、富足助航,预祝全州各族人民泼水节快乐!

"+
"

"+
"

"+
"

";



}
public  void  getInt(int index,String a){

int reLast= a.indexOf(">",index+indexcount);
if(reLast>0){
//String reStr= a.substring(index,reLast).trim();
String reStr= a.substring(index,index+indexcount+3).trim();
if(reStr.indexOf(">")>0){
reStr = reStr.substring(0,reStr.indexOf(">"));
}
String reStrId= reStr.substring(reStr.lastIndexOf("=")+1,reStr.length()).trim();
reMap.put(reStrId, reStr);
}
count= a.indexOf("/manage/infomanage/servlet/download?type=info&attachmentID=",index+indexcount);
if(count<0){
count= a.indexOf("/manage/infomanage/servlet/download?type=info&attachmentID=",index+indexcount);
if(count>=0){
indexcount=64;
}else{
indexcount=60;
}
}else{
indexcount=60;
}
//System.out.println(count+":"+cc+":"+bb+":"+dd);

}
public String  getReplaceAll(String a){
Iterator it = reMap.keySet().iterator();
String v=a;
while(it.hasNext()){
String key=(String)it.next();
v=v.replace(reMap.get(key),fileMap.get(key));
}
return v;
}

}



---commons-logging-1.1.1.jar

--httpclient-4.2.5.jar

--httpcore-4.2.4.jar

--json-lib-2.2.2-jdk13.jar

--jxl.jar

--log4j-1.2.16.jar

你可能感兴趣的:(java)