cancel
Showing results for 
Search instead for 
Did you mean: 

Java Exception after File Upload while retreiving using Filepath

Former Member
0 Kudos

Hello everyone,

I am performing the following steps:-

1) I am uploading a file from my local File system (D:/ a.XML) using FileUpload UI Element in Web Dynpro.

2) In the OnActionMethod of the View, I am trying to get the content of the file using the Filepath (D:/ a.XML).

But I am getting a Java Exception "No Such File or Directory" after deploying the archive and run on to the remote server.

The Exception is not a Web Dynpro Runtime Exception. But I am able to catch it in the try, catch block of the Java code.

Do I need the file to be present in the server as well. What is the Solution? Please advise me about, where is the content of the uploaded XML file stored and how can it be retrieved.

How can I retrieve the content of the file using a valid file path, because my method which will open the uploaded file requires argument as a filepath. Please suggest about this problem. I would be very thankful for your help.

Kind Regards,

Phani.

Edited by: Armin Reichert on Apr 18, 2008 2:43 PM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Please help me how can I do the following operations?

1)I am having an XML file in my local file system (client)..say D:/a.xml

2) The application lets the client to choose the path using "Browse" button in my application

3)The path in the client (D:/a.xml), which is a string is stored in the context

4) Now in "OnActionSubmit", I am using a Java API method, which I have imported, called "load" uses this path string to see the content of this file and manipulate it. For example,

Ontology o = new Ontology();

o.load("LOCAL FILE SYSTEM PATH THAT THE CLIENT HAVE CHOSEN USING BROWSE BUTTON");

How can I do this?

The objective of the application is very simple,

1)Let the client choose a file from his local file system using the "Browse" button

2)When he clicks Submit I need to put the contents of the file into my Java Object "Ontology" which uses the path in STEP 1 to populate the ONtology object.

The problem that is happening is,

1) I am able to select the local file system path and store it in a context using the "Browse" button

2) But When I try to populate my java object using the path it is searching that in the server with path such as,

/NWDI/usr_sap/JC92/j2ee/cluster/.......

I also used "absolute path" method to retrieve and use it. But it is giving the same path as above.

Please help me to solve this.

This is very very very urgent.

Former Member
0 Kudos

After uploading the file, you can create an input stream to read the file content. Doesn't your Ontology class allow reading from an input stream?

Armin

Former Member
0 Kudos

Hi there,

No , my Ontology object requires a path string to populate iteslf.

It doesn't read through contents from an Input Stream.

I'm having the file contents from the InputStream...using 'read' method..but my object requires a path string for itself to populate.

Somebody plz advice a roundabout?

thanks

Phani Gopal.

Former Member
0 Kudos

Hi,

Write to a file using normal java io and specify the path to your class

Regards,

Ayyapparaj

Former Member
0 Kudos

Hi Ayyaparaj,

Please have a look at the code that I am using for my application. I am getting the exception method after the load method i.e after this line of code "msgMgr.reportSuccess("\n\nInit0\n\n");". The exception that I am getting is "Invalid Stream Header".


  public void onActionMaximalJoin(
      com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent)
  {
    // @@begin onActionMaximalJoin(ServerEvent)
    MessageManager msgMgr = (MessageManager) wdThis.wdGetAPI().getComponent()
        .getMessageManager();

    InputStream text = null;
    int temp = 0;
    try
    {

      File file = new File(wdContext.currentContextElement().getUploadXML()
          .getResourceName().toString());
      FileOutputStream op = new FileOutputStream(file);

      if (wdContext.currentContextElement().getUploadXML() != null)
      {
        text = wdContext.currentContextElement().getUploadXML().read(false);
        while ((temp = text.read()) != -1)
        {
          op.write(temp);
        }
      }
      op.flush();
      op.close();
      String path = file.getAbsolutePath();
      wdComponentAPI.getMessageManager().reportSuccess(path);
      Ontology ontology = new Ontology();
      msgMgr.reportSuccess("\n\nInit0\n\n");
      ontology = Ontology.load(path);
      Lexicon mainLexicon = ontology.getMainLexicon();
      msgMgr.reportSuccess("\n\nInit1\n\n");

      text = null;
      temp = 0;
      File file1 = new File(wdContext.currentContextElement().getUploadGraph1()
          .getResourceName().toString());
      FileOutputStream op1 = new FileOutputStream(file1);

      if (wdContext.currentContextElement().getUploadGraph1() != null)
      {
        text = wdContext.currentContextElement().getUploadGraph1().read(false);
        while ((temp = text.read()) != -1)
        {
          op1.write(temp);
        }
      }
      op1.flush();
      op1.close();
      String path1 = file1.getAbsolutePath();
      wdComponentAPI.getMessageManager().reportSuccess(path1);
      CG cg1 = new CG();
      msgMgr.reportSuccess("\n\nInit2\n\n");
      cg1 = CG.load(path1);

      text = null;
      temp = 0;
      File file2 = new File(wdContext.currentContextElement().getUploadGraph2()
          .getResourceName().toString());
      FileOutputStream op2 = new FileOutputStream(file2);

      if (wdContext.currentContextElement().getUploadGraph2() != null)
      {
        text = wdContext.currentContextElement().getUploadGraph2().read(false);
        while ((temp = text.read()) != -1)
        {
          op2.write(temp);
        }
      }
      op2.flush();
      op2.close();
      String path2 = file1.getAbsolutePath();
      wdComponentAPI.getMessageManager().reportSuccess(path2);
      CG cg2 = new CG();
      msgMgr.reportSuccess("\n\nInit3\n\n");
      cg2 = CG.load(path2);
      CG maximalJoinResult = (CG) cg1.maximalJoin(cg2);
      StringBuffer output = new StringBuffer();
      output.append(maximalJoinResult.toString(mainLexicon));
      msgMgr.reportSuccess("\n\nInit4\n\n");
      System.out.println(output.toString());
      msgMgr.reportSuccess("\n\nInit5\n\n");
      msgMgr.reportSuccess(output.toString());
      msgMgr.reportSuccess("\n\nInit6\n\n");
    }

    catch (Exception e)
    {
      msgMgr.reportSuccess(e.getMessage());
    }
    // @@end
  }

Please advise me if you find any mistake.

Edited by: Armin Reichert on Apr 9, 2008 1:27 PM

Former Member
0 Kudos

First, I would not write all this code inside the action handler, but divide it into methods etc.

Try this: After uploading, write the data to a file in the server's temporary folder (see File.createTempFile()). Check if this file has the format expected by the Ontology class.

Armin

Former Member
0 Kudos

Hi,

I am getting an exception which is giving a message "Invalis Stream Header". I haven't got a clue about this exception. Can anyone advise me about this exception.

Kind Regards,

Phani.

Former Member
0 Kudos

>First, I would not write all this code inside the action handler, but divide it into methods etc.

>Try this: After uploading, write the data to a file in the server's temporary folder (see File.createTempFile()). >Check if this file has the format expected by the Ontology class.

Hi Armin,

Can you advise me in detail about writing the data to a file in the server's temporary folder using the File.createTemoFile() method. Because I did not really understand what you mean by that. Please advise me in detail.

Thanks a lot for your help,

Kind Regards,

Phani.

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi

Use this code to upload the file in the server and there by get the absolete path.

With the obselete path you can acces the content of the file uploaded.

public void onActionUpload_File_in_Server(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionUpload_File_in_Server(ServerEvent)
    InputStream text=null;
    int temp=0;
    try
    {
    	File file = new File(wdContext.currentContextElement().getFileResource().getResourceName().toString());
    	FileOutputStream op = new FileOutputStream(file);
    	
    	if(wdContext.currentContextElement().getFileResource()!=null)
    	{
    		text=wdContext.currentContextElement().getFileResource().read(false);
    		while((temp=text.read())!=-1)
    		{
    			op.write(temp);    			    			
    		}
    	}
    	op.flush();
    	op.close();
    	path = file.getAbsolutePath();
    	wdComponentAPI.getMessageManager().reportSuccess(path); 
    }
 
    catch(Exception e)
    {
    	e.printStackTrace();
    }
    //@@end
  }

Regards

Chandran S

Former Member
0 Kudos

Hi Chandran,

Thank you very much for your reply. I will try the code and will let you know.

Kind Regards,

Phani.

Former Member
0 Kudos

Hi Chandran,

In my application I need to upload a file everytime I run the application. So, is it necessary for the files that I am uploading to be present on the server? Can you also advise me, if I am using the UploadFile UI element then will the file that I am uploading gets stored on the Server automatically? Please advise me about this. I would be very very thankful for you.

Kind Regards,

Phani

Former Member
0 Kudos

Hi,

In my application I need to upload a file everytime I run the application?

I am not sure abt what you are planning to do here.

So, is it necessary for the files that I am uploading to be present on the server?

Its not manadatory that the files should be available on the server, it can be from the client system also.One think to rememeber is the path to the file should be correct.

if I am using the UploadFile UI element then will the file that I am uploading gets stored on the Server automatically?

It will be available as part of the context , you need to save it by using code.

Regards

Ayyapparaj