Showing results for 
Search instead for 
Did you mean: 

File Upload does not work.

Former Member
0 Kudos

Hi All,

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


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;

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();
				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())
								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
		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?


Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

In this line below

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

fileUpload.getFile() is coming as null and hence it is failing.

Any Ideas why this would be coming as null.

Former Member
0 Kudos

The problem was with the JSP.

This was missing ( encodingType="multipart/form-data" ) the blow tag

<hbj:form id="myFormId" encodingType="multipart/form-data" > and thi

This thread is closed.