[opencms-dev] Re: <input type="file" .../> File Uploads into OpenCMS apps

Armen Markarian markarian at inexweb.de
Wed Mar 31 11:09:02 CEST 2004


Hi .tom help is there :) look at this jsp code below

<%	
	String filter	   		= "";
	String resourcename		= "";
	String resourcepath		= "";
	String resourcetype	   	= "";
	String resrc			= "";
	String title 			= "";
	String navtext			= "";
	String desc				= "";
	String category			= "";
	String replaceFlag		= "";
	boolean cOK				= false;
	boolean rOK				= false;
	boolean replaceResource 	= false;
	boolean AlreadyExistsException   = false;
	boolean IllegalArgumentException = false;
	String this_filename 		= "";	
	
%>

<%-- ************************** END DECLARATIONS
************************************ --%>      
<%   
    	   		
	   	// resources you add to opencms must be publihed to be
accessable in online project
		// edit here the path where your resources are?
		Hashtable publishPath = new Hashtable();
		publishPath.put("image","/system/galleries/pics/");
		publishPath.put("binary","/system/galleries/download/");
		
	   // get req parameter	   
	   	filter		= req.getParameter("form_action");
		resourcename      =
req.getParameter("form_resourcename");
	   	resourcetype	= req.getParameter("form_type");   
		title 		= req.getParameter("form_title");
		navtext 		=
req.getParameter("form_navtext");
		category		=
req.getParameter("form_category");
		desc 			= req.getParameter("form_desc");
		replaceFlag		=
req.getParameter("form_replaceResource");
		
		if(title==null) 		title = "";
		if(navtext==null) 	navtext = "";
		if(category==null) 	category = "/system/galleries/";

		if(desc==null) 		desc = "";
		if(replaceFlag!=null && replaceFlag.equals("y"))
replaceResource = true;
		
		if(filter!=null && filter.equals("create")) {
			
			// create File	
			Hashtable properties = new Hashtable();
	
properties.put(I_CmsConstants.C_PROPERTY_TITLE,title);
	
properties.put(I_CmsConstants.C_PROPERTY_NAVTEXT,navtext);
	
properties.put(I_CmsConstants.C_PROPERTY_DESCRIPTION,desc);
		
		   	Enumeration files 		=
req.getFileNames();
		   	while(files.hasMoreElements()){
		   		this_filename 			=
(String) files.nextElement();
			   	byte[] filecontent 	=
req.getFile(this_filename);  
			   	long file_size = filecontent.length; 
			   	// variable C_MAX_ALLOWED_OBJECT_SIZE
defines how big the resources could be, in my case 4MB
				long 	C_MAX_ALLOWED_OBJECT_SIZE
= 4193280;
				if(file_size<C_MAX_ALLOWED_OBJECT_SIZE)
{
			   		try
						{

			
	
cmsObj.createResource(category, this_filename, resourcetype, properties,
filecontent);
	
cmsObj.publishResource((String)publishPath.get(resourcetype));

	
cmsObj.unlockResource(category);
	
cmsObj.publishResource(category+this_filename);
							resrc =
category+this_filename;							
					   	
					   		cOK = true; 
					}
					catch(Throwable t) {
						String searchStr1 =
"already exists";
	
if(t.toString().indexOf(searchStr1) != -1) {
	
AlreadyExistsException = true;
	
if(replaceResource) {
								byte[]
newfilecontent 	= req.getFile(this_filename);  
								try
	
{
	
filter = "replace";
	
cmsObj.lockResource(category+this_filename);
	
cmsObj.replaceResource(category+this_filename, resourcetype, properties,
newfilecontent);
	
cmsObj.publishResource((String)publishPath.get(resourcetype));
	
cmsObj.unlockResource(category);
	
cmsObj.publishResource(category+this_filename);

	
rOK = true;
								}
	
catch(Throwable t2) {
	
System.out.println(GetLogFileTime() + " [ERR]: " + reqContext.getUri() +
" -> " + t2);
								}
							}
						}

					}
				} else {
					IllegalArgumentException = true;
				}	
			}		
		}

%>

Regards 

Armen

** opencms >> the best java-framework i ever seen **




More information about the opencms-dev mailing list