Skip to Content
0
Former Member
Jul 17, 2008 at 03:07 PM

File Upload does not work.

18 Views

Hi All,

Im trying to build a file upload utility. Following is my code:


import java.io.Serializable;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.sapportals.htmlb.event.Event;
import com.sapportals.htmlb.rendering.IPageContext;
import com.sapportals.htmlb.rendering.PageContextFactory;

import com.sapportals.htmlb.FileUpload;

import com.sapportals.portal.prt.component.*;
import com.sapportals.wcm.repository.AccessDeniedException;
import com.sapportals.wcm.repository.Content;
import com.sapportals.wcm.repository.ICollection;
import com.sapportals.wcm.repository.IResource;
import com.sapportals.wcm.repository.NotSupportedException;
import com.sapportals.wcm.repository.ResourceContext;
import com.sapportals.wcm.repository.ResourceException;
import com.sapportals.wcm.repository.ResourceFactory;
import com.sapportals.wcm.util.uri.RID;
import com.sapportals.portal.security.usermanagement.IUser;


public class UploadUtility implements  Serializable{
	
		protected IPortalComponentRequest request;
		protected IPortalComponentResponse response;
		protected IPortalComponentSession session; 
		protected IPortalComponentContext context;
		protected IPortalComponentProfile profile;
		protected String userId;
		protected String fileName;
		private DAO DAO = null;
	
		private final static int INITIAL_STATE = 0;    
		private final static int ERROR_STATE = 1;  
		private int state = INITIAL_STATE;
    
    
		public void uploadUtility(IPortalComponentRequest request, IPortalComponentResponse response)
		{
    	
			String mimetype;
			String resourceName;
			String repository;
			FileInputStream sourceFileInput = null;
		
			session = request.getComponentSession();
			    
			DAO = new DAO();
			DAO = (DAO)session.getValue("DAO");
			IUser epUser=DAO.getEpUser();
		
			System.err.println("berry + Getting Iuser from inside UploadUtility" + epUser);
		
		/*	IWDClientUser wdUser = WDClientUser.getCurrentUser();
			IUser user = wdUser.wdUser.getSAPUser(); */ 

		
		//	IUser epUser=(IUser)request.getUser().getUser();
			System.err.println("epUser - " + epUser);
			ResourceContext ctx = new ResourceContext(epUser);
			repository = "/Folder1/newfolder";
			RID rid=RID.getRID(repository);
			System.err.println("getting RID :- " + rid);
			IPageContext context = PageContextFactory.createPageContext(request,response);

		//	PageContext context = PageContextFactory.createPageContext(request, response);
			Event event=context.getCurrentEvent();
		
			if(null!=event)
			{
				String event_name=event.getAction();
				if(null!=event_name && event_name.trim().equalsIgnoreCase("upload_file"))
					{
					 FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");
					 System.err.println("tester0 " + fileUpload.getUI());
						if(null !=fileUpload && null!=fileUpload.getFile())
								{
								
									System.err.println("tester1");
								mimetype = fileUpload.getFile().getContentType();
								resourceName = fileUpload.getFile().getFileName();
								System.err.println("resourceName inside fileUploadUtility :- " + resourceName);
				
								//sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
							
								fileName = resourceName;
								System.err.println("filename :- " + fileName);
							
								try {
									sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
								} catch (FileNotFoundException fnf) {
									state = ERROR_STATE;
									System.err.print("FileNotFoundException Occured " + fnf.toString()); 
								}
							
							
								Content content = new Content(sourceFileInput, mimetype, -1L);
							
								IResource resource = null;
								try {
									resource =(IResource) (ResourceFactory.getInstance().getResource(rid, ctx));
								} catch (ResourceException resexp) {
									state = ERROR_STATE;
									System.err.print("Resource Exception Occured for IResource" + resexp.toString()); 	
								}
							
							
								ICollection aCollection=(ICollection)resource;
								//IResource newResource = aCollection.createResource(resourceName, null, content);
								try {
									IResource newResource = (IResource) aCollection.createResource(resourceName, null, content);
								} catch (NotSupportedException e2) {
									state = ERROR_STATE;
									System.err.print("Not Supported Exception Occured" + e2.toString());
								} catch (AccessDeniedException e2) {
									state = ERROR_STATE;
									System.err.print("Access Denied Exception Occured" + e2.toString());
								} catch (ResourceException e2) {
									state = ERROR_STATE;
									System.err.print("Resource Exception Occured for ICollection" + e2.toString()); 	
								}//end of catch
					}//end of innermost if
			}//end of 2nd if
		}//end of 1st If
	
		DAO.setFileName(fileName);
		DAO.setState(state);
		session.putValue("DAO", DAO); 
    	
	  }// end of uploadUtil


}


When I run this code, it doesnt go into this "IF" block:

if(null !=fileUpload && null!=fileUpload.getFile())

And hence doesnt upload file.

Can anyone suggest whats going wrong?

Thanks