我想从"FolderPath"列下的 excel 表中读取和获取我的文件夹路径,并在名为"Documentum"的父文件夹下创建一个文件夹结构列表。我已附上示例 excel 数据,如下所示。我可以通过硬编码要创建的文件夹路径从下面的代码创建一个文件夹。例如,在这种情况下它是:/documentum。
文件夹路径
/documentum/folder1
/documentum/folder2
/documentum/folder3
/documentum/folder8/folder9
**
请建议,如何在下面的代码中添加此功能。我尝试使用 excel POI 但没有运气,因为我在 POI 中工作不多,对以下代码的任何示例修改或建议都会有所帮助。
package com.documentum;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;
import com.documentum.fc.common.IDfLoginInfo;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import java.util.StringTokenizer;
import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
public class AutoFolderStructure
{
/**
* @param args
*/
public static void main(String[] args)
{
String username = "user";
String password = "docu";
String repoName = "documentum";
String folderPath = "/documentum"; //folder path to be created
IDfSessionManager sessMgr = null;
IDfSession sess = null;
try
{
sessMgr = createSessionManager();
addIdentity(sessMgr,username,password,repoName);
sess = sessMgr.getSession(repoName);
IDfId newId = createFolder(sess,folderPath,null,null,null);
System.out.println("Created folder: " + newId);
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if((sessMgr != null) && (sess != null))
{
sessMgr.release(sess);
}
}
}
public static IDfId createFolder(
IDfSession sess,
String path,
String pathSep,
String type,
String basePath)
throws DfException
{
boolean cabinetStillToBeProcessed = true;
if (pathSep == null || (pathSep.length() == 0))
{
pathSep = "/";
}
if ((type == null) || (type.length() == 0))
{
type = "dm_folder";
}
IDfId currentId = null;
StringBuffer bufFldrPath = new StringBuffer(48);
if ((basePath != null) && (basePath.length() > 1))
{
currentId = getIdByPath(sess, basePath);
if (!currentId.isNull())
{
//base path actually exists.
bufFldrPath.append(basePath);
cabinetStillToBeProcessed = false; //cabinet already processed due to base path.
int basePathLen = basePath.length();
if(basePathLen < path.length())
{
path = path.substring(basePath.length());
}
}
}
StringTokenizer tokFldrNames = new StringTokenizer(path, pathSep);
if (cabinetStillToBeProcessed)
{
//Execution will come here only if basePath was not specified or
//if specified basePath was not valid.
String cabinetName = tokFldrNames.nextToken();
StringBuffer cabQual = new StringBuffer(32);
cabQual.append("dm_cabinet where object_name='").append(
cabinetName).append(
"'");
currentId = sess.getIdByQualification(cabQual.toString());
if (currentId.isNull())
{
//need to create cabinet.
IDfFolder cab = (IDfFolder) sess.newObject("dm_cabinet");
cab.setObjectName(cabinetName);
cab.save();
currentId = cab.getObjectId();
}
bufFldrPath.append(pathSep).append(cabinetName);
}
//By this point the bufFldrPath will either have the cabinet path
//or it will have the basePath in it.
//now create all folders beyond the cabinet or basePath.
while(tokFldrNames.hasMoreTokens())
{
String parentPath = bufFldrPath.toString();
String fldrName = tokFldrNames.nextToken();
bufFldrPath.append(pathSep).append(fldrName);
//by this point the buffer should contain the new expected path
currentId = getIdByPath(sess,bufFldrPath.toString());
if(currentId.isNull())
{
//looks like the new folder in the path does not exist.
IDfFolder newFldr = (IDfFolder) sess.newObject(type);
newFldr.setObjectName(fldrName);
newFldr.link(parentPath);
newFldr.save();
currentId = newFldr.getObjectId();
}
//by this point currentId should point to next folder in path
}//while(all folder names)
return currentId;
}
public static IDfId getIdByPath(IDfSession sess, String path)
throws DfException
{
int pathSepIndex = path.lastIndexOf('/');
if (pathSepIndex == -1)
{
return new DfId("000");
}
StringBuffer bufQual = new StringBuffer(32);
if (pathSepIndex == 0)
{
//its a cabinet path
bufQual.append(" dm_cabinet where object_name='");
bufQual.append(path.substring(1));
bufQual.append("'");
}
else
{
bufQual.append(" dm_sysobject where FOLDER('");
bufQual.append(path.substring(0, pathSepIndex));
bufQual.append("') ");
bufQual.append(" and object_name='");
bufQual.append(path.substring(pathSepIndex + 1));
bufQual.append("'");
}
String strQual = bufQual.toString();
IDfId id = sess.getIdByQualification(strQual);
return id;
}
private static IDfSessionManager createSessionManager() throws DfException
{
IDfClientX clientX = new DfClientX();
IDfClient localClient = clientX.getLocalClient();
IDfSessionManager sessMgr = localClient.newSessionManager();
return sessMgr;
}
private static void addIdentity(IDfSessionManager sm, String username,
String password, String repoName) throws DfException
{
IDfClientX clientX = new DfClientX();
IDfLoginInfo li = clientX.getLoginInfo();
li.setUser(username);
li.setPassword(password);
// check if session manager already has an identity.
// if yes, remove it.
if (sm.hasIdentity(repoName))
{
sm.clearIdentity(repoName);
}
sm.setIdentity(repoName, li);
}
}
谢谢和问候德布