opencms批量修改filetype和filename

注意:我下面的程序是在数据迁移的事候,用来批量修改自定义结构化页面的,在批量修改filetype的时候请确保,修改后的模版xsd与修改前的一样,否则出现问题别找我。

 

代码如下:

CmsJspActionElement cms = new CmsJspActionElement(pageContext,request,response);

CmsObject cmso = cms.getCmsObject();
			List list = cmso.getResourcesInFolder(folderName, CmsResourceFilter.ALL);
			Iterator it = list.iterator();
			while(it.hasNext()){
				CmsResource resource = (CmsResource) it.next();
				if(resource.isFolder()){
					continue;
				}
				String target = resource.getRootPath();
				out.println(changeFileType(cms, target, newType));

...

/*
* 修改文件的file type属性
* 参数:target是目标文件的路径(绝对)
*/
public String changeFileType(CmsJspActionElement cms, String target, int newTypeId){
	String rst = "";
	Locale locale = Locale.ENGLISH;
	try{
		CmsObject cmso = cms.getCmsObject();
		cmso.lockResource(target);
		CmsResource resource = cmso.readFile(target, CmsResourceFilter.ALL);
		resource.setType(newTypeId);
		cmso.writeResource(resource);
		cmso.unlockResource(target);
		rst += target+" modify filetype to "+newTypeId+" success.<br/>";
	}catch(Exception e){
		rst+= e.getMessage()+"<br/>";
		e.printStackTrace();
	}
	return rst;
}

 

批量修改文件名,也可以用来批量移动文件:

CmsJspActionElement cms = new CmsJspActionElement(pageContext,request,response);

...

List list = cmso.getResourcesInFolder(folderName, CmsResourceFilter.ALL);
			Iterator it = list.iterator();
			while(it.hasNext()){
				CmsResource resource = (CmsResource) it.next();
				if(resource.isFolder()){
					continue;
				}
				String source = resource.getRootPath();
				String fileName = resource.getName();
				
				String target = folderName+"/"+fileName.replace("aa","bb");
				out.println(changeFileName(cms, source, target));

...

/*
* batch修改文件name
* 参数:target是目标文件的路径(绝对)
*/
public String changeFileName(CmsJspActionElement cms, String source, String target){
	String rst = "";
	Locale locale = Locale.ENGLISH;
	try{
		CmsObject cmso = cms.getCmsObject();
		cmso.lockResource(source);
		CmsResource resource = cmso.readFile(source, CmsResourceFilter.ALL);
		cmso.moveResource(source,target);
		rst += "raname file:"+source+" to "+target+" success.<br/>";
	}catch(Exception e){
		rst+= e.getMessage()+"<br/>";
		e.printStackTrace();
	}
	return rst;
}
 

你可能感兴趣的:(cms)