Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 26
Registered: ‎03-26-2010
My Device: 8520
My Carrier: 1

Upload a file to server

[ Edited ]

Hi,

I would to like to get some samples for file uploading to server if that perticular file is not present ,otherwise it should update that perticular file on server.

 

Regards

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload a file to server

What have you looked at so far?

 

Have you searched the forum for similar questions?

Contributor
Posts: 26
Registered: ‎03-26-2010
My Device: 8520
My Carrier: 1

Re: Upload a file to server

I am able to read the file but when i am writing it to the specified location,it does not write the contents.

Sample of code of how i am uploading the file.

I am trying it on a Simulator.

 

 StreamConnection s = null;
 s = (StreamConnection)Connector.open(url+filename,Connector.READ_WRITE);
 HttpConnection httpConn = (HttpConnection)s;
 int status = httpConn.getResponseCode();
        if (status == HttpConnection.HTTP_OK)
        {
         OutputStream os = (OutputStream)httpConn.openOutputStream();
         os.write(data,0,data.length);
         os.flush();
         os.close();
        }
        else
         System.out.println("No Connection");

        httpConn.close();
     }
     catch (IOException e)
     {                       
         System.out.println(e.toString());
     } 

 

where data is a byte array.What is wrong in this code or is there some different way to upload file to server.

 

 

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload a file to server

I'm guessing you are pretty new to the world of HTTP communication.  I suggest you do some research on how it works.

 

IN this case, you should probably look at using an HTTP POST to send you data, there is a sample of an HTTP POST included with the API documentation for HttpConnection. 

 

But you will need a matching Server component, that takes the POST data and writes it to a file.  The BlackBerry is only one end of the pipe and there is nothing that is automatically at the other end to write the data.  You have to create both ends.