Write a file programatically

(Difference between revisions)
Jump to: navigation, search
 
(added sample code)
Line 1: Line 1:
Try looking at CmsNewResourceUpload.java from the OpenCms sourcecode.  It's available from opencms.org via an interactive html browser.
+
How to write a file programatically
 +
 
 +
//ensure we're in the offline project.
 +
CmsRequestContext cmsContext = cmsObj.getRequestContext();
 +
CmsProject curProject = cmsContext.currentProject();
 +
if(curProject.isOnlineProject()){
 +
      CmsProject offlineProject = cmsObj.readProject("Offline");
 +
      cmsContext.setCurrentProject(offlineProject);
 +
}
 +
 
 +
byte [] content = your data
 +
List properties = list of CmsProperties
 +
 
 +
//get the fixed up filename as shown in CmsNewResourceUpload
 +
String newResname = cmsObj.getRequestContext().getFileTranslator().translateResource(path + filename);
 +
 
 +
int resTypeId = OpenCms.getResourceManager().getDefaultTypeForName(newResname).getTypeId();
 +
 
 +
cmsObj.createResource(newResname, resTypeId, content, properties);
 +
cmsObj.unlockResource(newResname);
 +
cmsObj.publishResource(newResname);
 +
 
 +
 
 +
 
 +
 
 +
You can also try looking at CmsNewResourceUpload.java from the OpenCms sourcecode.  It's available from opencms.org via an interactive html browser.

Revision as of 00:31, 15 June 2007

How to write a file programatically

//ensure we're in the offline project.
CmsRequestContext cmsContext = cmsObj.getRequestContext();
CmsProject curProject = cmsContext.currentProject();
if(curProject.isOnlineProject()){
      CmsProject offlineProject = cmsObj.readProject("Offline");
      cmsContext.setCurrentProject(offlineProject);
}
byte [] content = your data
List properties = list of CmsProperties 
//get the fixed up filename as shown in CmsNewResourceUpload
String newResname = cmsObj.getRequestContext().getFileTranslator().translateResource(path + filename);
int resTypeId = OpenCms.getResourceManager().getDefaultTypeForName(newResname).getTypeId();
cmsObj.createResource(newResname, resTypeId, content, properties);
cmsObj.unlockResource(newResname);
cmsObj.publishResource(newResname);



You can also try looking at CmsNewResourceUpload.java from the OpenCms sourcecode. It's available from opencms.org via an interactive html browser.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox