Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Problem with uploading/downloading from KM

I have a requirement wherein I need to upload / download files to and from a KM server through webdynpro.

I am uploading files to the KM folder documents inside which I am creating my folder structure as AUT/"Some Appraisal No"/"Row Index"

For eg. <b>documents/AUT/762957924875/0</b> and uploading one file into this leaf folder.

I am successfully able to upload / download files but have problems with the format of the file . When I try to download the file it shows me strange characters instead of the original file .

Could anyone please provide any pointers to this ??????

I have pasted the code for upload and download below .

<b>Upload :</b>

public void onActionUpload(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionUpload(ServerEvent)
//	Getting the message manager
	IWDMessageManager msgMgr = wdThis.wdGetAPI().getComponent().getMessageManager();
	String strAppraisalID = "";
	RID objRid = null;
	RID objNewRid = null;
	RID objRid1 = null;
	RID objNewRid1 = null;
	RID objNewRid2 = null;
	RID objRid2 = null;
	RID objNewRid3 = null;
	IResourceFactory objFactory = null;
	String strLocation = "";
	String strPath = "/documents";
	ICollection objRootFolder = null;
	ICollection objAUTFolder = null;
	ICollection objAppraisalFolder = null;
	ICollection objAppraisalFolderActual = null;
	ICollection objRowwiseFolder = null;
    ICollection objRowwiseFolderActual = null;
	ResourceContext objResourceContext = null;
	String strFileName = "";
	File objFile = null;
	FileOutputStream objFileOutputStream = null;
	FileInputStream objFileInputStream = null;
	ByteArrayInputStream objByteArrayInputStream = null;
	IResource objFileResource = null;
	IResource objResource1 = null;
	IResource objResourceCheckRowwiseFolder = null;
	IResource objResourceCheckFolderAUT = null;
	IResource objResourceCheckFolderAppraisal = null;
	Content objContent = null;
	IWDClientUser objUser = null;
	com.sap.security.api.IUser objSAPUser = null;
	IUser ep5User = null;
	
	int intRowIndex = 0;
	int intSizeOfFolder = 0;
	try
	{
		
		strAppraisalID = wdContext.currentContextElement().getCtx_AppraisalID();
		strFileName = wdContext.currentContextElement().getCtx_FileName();
		intRowIndex = wdContext.currentContextElement().getCtx_RowIndex();		
		strLocation = strPath+"/"+"AUT"+"/"+strAppraisalID+"/"+intRowIndex;
		
		objRid = RID.getRID(strPath);
		objNewRid = RID.getRID(strLocation);
		objNewRid1 = RID.getRID(strPath+"/"+"AUT");
		objNewRid2 = RID.getRID(strPath+"/"+"AUT"+"/"+strAppraisalID);
		
		objUser = WDClientUser.getCurrentUser();
		objSAPUser = objUser.getSAPUser();		
		ep5User = WPUMFactory.getUserFactory().getEP5User(objSAPUser); 		
		objResourceContext = new ResourceContext(ep5User);		
		objFactory = ResourceFactory.getInstance(); 
		
		objResourceCheckRowwiseFolder = objFactory.getResource(objNewRid,objResourceContext);
		objResourceCheckFolderAUT = objFactory.getResource(objNewRid1,objResourceContext);
		objResourceCheckFolderAppraisal = objFactory.getResource(objNewRid2,objResourceContext);
		
		objRootFolder = (ICollection) objFactory.getResource(objRid,objResourceContext);
		if(objResourceCheckFolderAUT==null)
		{
		   objAUTFolder = objRootFolder.createCollection("AUT",null,null);
		   objRid1 = RID.getRID(strPath+"/"+"AUT");
		   objAppraisalFolder = (ICollection) objFactory.getResource(objRid1,objResourceContext);
		   objAppraisalFolderActual = objAppraisalFolder.createCollection(strAppraisalID,null,null); 
		   objRid2 = RID.getRID(strPath+"/"+"AUT"+"/"+strAppraisalID);
		   objRowwiseFolder = (ICollection) objFactory.getResource(objRid2,objResourceContext);
		   objRowwiseFolderActual = objRowwiseFolder.createCollection(""+intRowIndex,null,null);
		}
		else if(objResourceCheckFolderAppraisal==null)
		{
			objAUTFolder = (ICollection)objResourceCheckFolderAUT;
			objRid1 = RID.getRID(strPath+"/"+"AUT");
			objAppraisalFolder = (ICollection) objFactory.getResource(objRid1,objResourceContext);		   
			objAppraisalFolderActual = objAppraisalFolder.createCollection(strAppraisalID,null,null); 
		    objRid2 = RID.getRID(strPath+"/"+"AUT"+"/"+strAppraisalID);
			objRowwiseFolder = (ICollection) objFactory.getResource(objRid2,objResourceContext);
			objRowwiseFolderActual = objRowwiseFolder.createCollection(""+intRowIndex,null,null);
		}
		else if(objResourceCheckRowwiseFolder==null)
		{
			objAppraisalFolderActual = (ICollection)objResourceCheckFolderAppraisal;
			objRid2 = RID.getRID(strPath+"/"+"AUT"+"/"+strAppraisalID);
			objRowwiseFolder = (ICollection) objFactory.getResource(objRid2,objResourceContext);
			objRowwiseFolderActual = objRowwiseFolder.createCollection(""+intRowIndex,null,null);
		}
		else 
		{			
			objRowwiseFolderActual = (ICollection)objResourceCheckRowwiseFolder;			
			objRowwiseFolderActual.delete();
			objRid2 = RID.getRID(strPath+"/"+"AUT"+"/"+strAppraisalID);
			objRowwiseFolder = (ICollection) objFactory.getResource(objRid2,objResourceContext);
			objRowwiseFolderActual = objRowwiseFolder.createCollection(""+intRowIndex,null,null);					
		}
		
		objFile = new File(strFileName); 
	   	objFileOutputStream = new FileOutputStream(objFile);
	   	if(wdContext.currentContextElement().getCtx_FileUpload()!= null)
	   	{		
		   objFileOutputStream.write(wdContext.currentContextElement().getCtx_FileUpload());
		}
		objFileOutputStream.close();
		
 
	    objFileInputStream = new FileInputStream(strFileName);
		
	    objFileInputStream.read(); 
		objContent = new Content(objFileInputStream,binaryType.getMimeType().getHtmlMime(),-1);
		
		try
		{		
			objFileResource = objRowwiseFolderActual.createResource(strFileName,null,objContent);
			msgMgr.reportSuccess("Upload Successful");
		}
		catch(NameAlreadyExistsException e)
		{
			msgMgr.reportException("File with this name exists . Please upload a different file",true);
		}		
	    objFileInputStream.close(); 
		objFile.delete(); 
				 
			}
	
	catch(Exception e) 
	{
		//Getting the message
		String msg = e.getLocalizedMessage();
		
		//Checking for null values and zero length of message 
		if ((msg==null) || (msg.length()==0))
		{
			msg = e.getMessage();
		}//end of if ((msg==null) || (msg.length()==0))
		if ((msg == null) || (msg.length() == 0)) 
		{
			//Converting the message into string
			msg = e.toString();
		}//end of if ((msg==null) || (msg.length()==0))
		
		//Reporting the exception
		msgMgr.reportException(msg,true);
	 }//end of catch
    //@@end
  }

<b>Download:</b>

  public void DownloadFile( )
  {
    //@@begin DownloadFile()
	IWDMessageManager msgMgr = wdThis.wdGetAPI().getComponent().getMessageManager();
	String strAppraisalID = "";
	RID objRid = null;
	RID objNewRid = null;
	RID objRid1 = null;
	RID objNewRid1 = null;
	RID objNewRid2 = null;
	RID objRid2 = null;
	RID objNewRid3 = null;
	IResourceFactory objFactory = null;
	String strLocation = "";
	String strPath = "/documents";
	ICollection objRootFolder = null;
	ICollection objAUTFolder = null;
	ICollection objAppraisalFolder = null;
	ICollection objAppraisalFolderActual = null;
	ICollection objRowwiseFolder = null;
	ICollection objRowwiseFolderActual = null;
	ResourceContext objResourceContext = null;
	String strFileName = "";
	File objFile = null;
	ByteArrayOutputStream objFileOutputStream = null;
	InputStream objFileInputStream = null;
	IResource objFileResource = null;
	IResourceList objResourceList = null;
	IResource objResource1 = null;
	IResource objResourceCheckRowwiseFolder = null;
	IResource objResourceCheckFolderAUT = null;
	ICollection objResourceCheckFolderAppraisal = null;
	IResourceListIterator objResourceListIterator = null;
	IResource objTempResource = null;
	IContent objContent = null;
	IWDClientUser objUser = null;
	com.sap.security.api.IUser objSAPUser = null;
	IUser ep5User = null;
	int intRowIndex = 0;
	int intSizeOfFolder = 0;
	URI objURI = null;
	IHierarchicalUri objURIReference = null;
	InputStream objInputStream = null;
	try
	{
		
		strAppraisalID = wdContext.currentContextElement().getCtx_AppraisalID();
		intRowIndex = wdContext.currentContextElement().getCtx_RowIndex();		
		strLocation = strPath+"/"+"AUT"+"/"+strAppraisalID+"/"+intRowIndex;
		
		objNewRid2 = RID.getRID(strLocation);
		
		objUser = WDClientUser.getCurrentUser();
		objSAPUser = objUser.getSAPUser();		
		ep5User = WPUMFactory.getUserFactory().getEP5User(objSAPUser); 		
		
		objResourceContext = new ResourceContext(ep5User);		
		objFactory = ResourceFactory.getInstance(); 
		
		objResourceCheckFolderAppraisal = (ICollection)objFactory.getResource(objNewRid2,objResourceContext);
		
		if (objResourceCheckFolderAppraisal!=null)
		{
			objResourceList = objResourceCheckFolderAppraisal.getChildren();
		}
		
		if (objResourceList!=null)
		{
			objResourceListIterator = objResourceList.listIterator();
			while (objResourceListIterator.hasNext())
			{
				//binaryType.setMimeType(WDWebResourceType.XLS);
				
				
				objTempResource = objResourceListIterator.next();
				strFileName = objTempResource.getDisplayName();
				//objURI = objTempResource.getAccessURI();
			
				//msgMgr.reportSuccess("URI OBJECT   "+objURI.toString());
				if((strFileName==null) || (strFileName.length()==0))
				{
					strFileName = objTempResource.getName();
					if((strFileName!=null) && (strFileName.length()!=0))
					{ 
						if (strFileName.toLowerCase().endsWith(".doc"))
						{
							binaryType.setMimeType(WDWebResourceType.DOC);
						}
						else if ((strFileName.toLowerCase().endsWith(".xls")) ||
							(strFileName.toLowerCase().endsWith(".xlt")))
						{
							binaryType.setMimeType(WDWebResourceType.XLS);
						}
						else if (strFileName.toLowerCase().endsWith(".pdf"))
						{
							binaryType.setMimeType(WDWebResourceType.PDF);
						}
						else if (strFileName.toLowerCase().endsWith(".ppt"))
						{
							binaryType.setMimeType(WDWebResourceType.PPT);
						}
						else if (strFileName.toLowerCase().endsWith(".gif"))
						{
	
							binaryType.setMimeType(WDWebResourceType.GIF_IMAGE);
						}
						else if (strFileName.toLowerCase().endsWith(".jpg"))
						{
	
							binaryType.setMimeType(WDWebResourceType.JPG_IMAGE);
						}
						else
						{
							binaryType.setMimeType(WDWebResourceType.TXT);
						}
					}
					msgMgr.reportSuccess("MIME DOWNLOAD   "+binaryType.getMimeType().getHtmlMime());
					objContent = objTempResource.getContent();
					objInputStream = objContent.getInputStream();//
					int intInputDataLength = objInputStream.available();
					msgMgr.reportSuccess("BYTE Data SIZE while Downloading::"+intInputDataLength);
					wdContext.currentContextElement().setCtx_FileResource(
					this.getByteArrayFromResourcePath(objInputStream));
					binaryType.setFileName(strFileName);
					msgMgr.reportSuccess("File Name while Downloading::"+strFileName);
 
					
				}
				
			}
		}
	}
	catch(Exception e) 
	{
		//Getting the message
		String msg = e.getLocalizedMessage();
		
		//Checking for null values and zero length of message 
		if ((msg==null) || (msg.length()==0))
		{
			msg = e.getMessage();
		}//end of if ((msg==null) || (msg.length()==0))
		if ((msg == null) || (msg.length() == 0)) 
		{
			//Converting the message into string
			msg = e.toString();
		}//end of if ((msg==null) || (msg.length()==0))
		
		//Reporting the exception
		msgMgr.reportException(msg,true);
	 }//end of catch
    //@@end
  }
 
 
 private byte[] getByteArrayFromResourcePath(InputStream in)
  throws FileNotFoundException, IOException {
 
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	//FileOutputStream out = new FileOutputStream();
	int intInputDataLength = 0;
	byte[] part = new byte[in.available()];
	int nextByte = in.read(part);
	in.close();
	out.write(part);
		
  return out.toByteArray();
  }

Former Member
Not what you were looking for? View more on this topic or Ask a question