File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes File upload in J2ME Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "File upload in J2ME" Watch "File upload in J2ME" New topic
Author

File upload in J2ME

wand drew
Greenhorn

Joined: May 26, 2011
Posts: 6
Hello,
I need some help for my final project. I have to upload a file (audio file) to a http or ftp server from my mobile application. Can anybody help me with an advise or with an example on how to upload a file to a http/ftp server in j2me? I search a lot and I tried many things, but I don't know how to do it, that's why an example would be great.
Thank you!
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
FTP isn't a connection type in Java ME but HTTP is.

The Package javax.microedition.io contains interfaces that you need to implement an HTTP connection, specifically cldc 1.0 has Interface HttpConnection, and if you target cldc 1.1 there's also HTTPS, s for secure, Interface HttpsConnection.

In order to implement the interfaces in the javax.microedition.io.* package, You need to use a static method call from your concrete methods to the methods in the Connector class.

If you don't know how to make a static method call, it's just [class name][dot][method name], or like:
Then, after you implement the interface, you can hook it up to the stream classes in Package java.io to help you import or export data.

You can also find an example : How to send POST data to a web server - Forum Nokia Wiki

As for the web side of the upload, you will probably need a server-side script on the website to upload things via http.
wand drew
Greenhorn

Joined: May 26, 2011
Posts: 6
Thanks Walter for your help, but I didn't succeed yet.
Here is what I did:

recordedAudioArray contains the bytes from the file that I want to upload on http server.
Doing this, the file is not saved to my folder (htdocs) on http server. Maybe I don't understand very well, but I don't know how to do it.
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
You are posting to HTTP and you need to have a Servlet or other script running at the destination URL to accept the file. This would be external to your application, running on the web server. It is unlikely that you have the required web services running on your mobile device to test it locally (localhost url).
wand drew
Greenhorn

Joined: May 26, 2011
Posts: 6
I'm testing on Wireless Toolkit emulators, so it should work with localhost. I hope I can do that script, even if I don't know anything about this now
wand drew
Greenhorn

Joined: May 26, 2011
Posts: 6
Walter Gabrielsen Iii wrote:You are posting to HTTP and you need to have a Servlet or other script running at the destination URL to accept the file. This would be external to your application, running on the web server.


I searched a bit for a php script and I tried this:



The post.php code:

But it's not working. I get the message: "There was an error, try again!". Walter, can you help me please?
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
That PHP script looks like it's for an upload form that goes on a web page. It's expecting more than just the file, like a file name. You will probably need to write your own customized upload script to meet your needs.

For the Server side, If you're going to use PHP then read the documentation: Handling file uploads.

For the Java ME side, There is additional help from Nokia wiki: Mobile Design Pattern: File Upload (Scroll down to the Java ME section).
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
I just got your PM regarding the trouble you're having with the server side script. I can't really help you with the code because it's above my head too, I not having written an upload script before.

Ok, I can tell that you aren't sending enough information to the PHP upload script that you posted last. You need to upload several things, such as a file name, See the example: HTTP Post multipart file upload with Java ME. -- be warned their PHP example only reports the file size, and doesn't save the file.



However, if you really want help you should ask someone who knows more about networking, specifically about the topic of what you need to send to the server. Perhaps, go to the Servlet section of the form, or ask at a PHP forum about what functions you need to use.
wand drew
Greenhorn

Joined: May 26, 2011
Posts: 6
Ok, Walter. Anyway, thanks for your help!
I tried also with that HttpMultipartRequest and with their php script, but unfortunately, it didn't report the file size or anything...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File upload in J2ME