[转][opencms]移动文件的简单示例

下面的简单示例实现了将目录/home01/中的多于8个的文件移动到目录/home01/more/中
在添加新的信息时,导航位置选择"---插在首位---",这样保存最新的8条信息还保留在/home01/中
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="org.opencms.jsp.*" %>
<%@ page import="org.opencms.file.CmsResource" %>
<%@ page import="org.opencms.file.CmsResourceFilter" %>
<%

String sourceFolder="/home01/";
String targetFolder="/home01/more/";

CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
org.opencms.file.CmsObject cmsObject = cms.getCmsObject();
CmsResource thisResource = null;
java.util.List resourceVector = cms.getNavigation().getNavigationForFolder(sourceFolder);
cmsObject.lockResource(sourceFolder);
System.out.println(resourceVector.size());
for (int fileCounter = resourceVector.size()-9; fileCounter >= 0; fileCounter--) {
CmsJspNavElement tmpNe = (CmsJspNavElement ) resourceVector.get(fileCounter);
thisResource = (CmsResource)cmsObject.readFile(tmpNe.getResourceName()) ;
System.out.println(thisResource.getState());
try {
//out.println("<br>文件-----"+tmpNe.getFileName());
cmsObject.moveResource(sourceFolder+ thisResource.getName(),targetFolder +thisResource.getName());

} catch (Exception e) {
out.println("<br><font color=#ff0000>处理文件: " +thisResource.getName() +"出现异常!</font>");
}
}
cmsObject.unlockResource(sourceFolder);
cmsObject.publishResource(sourceFolder);

%>

你可能感兴趣的:(java,cms,jsp)