This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes send image in post method from J2ME to Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "send image in post method from J2ME to Servlet" Watch "send image in post method from J2ME to Servlet" New topic
Author

send image in post method from J2ME to Servlet

akash kubavat
Greenhorn

Joined: May 14, 2010
Posts: 2
Hi,

I want to send Image from J2ME to SERVLET. I am able to convert image into Byte Array, and send by Http POST.

I have coded as :
- From Mobile :
conn = (HttpConnection)Connector.open(url,Connector.READ_WRITE,true);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
os.write(bytes, 0, bytes.length);//bytes = byte array of image

- At servlet :
String line;
BufferedReader r1 = new BufferedReader(new InputStreamReader(in));
while ((line = r1.readLine()) != null)
{
System.out.println("line=" + line);
buf.append(line);
}
String s = buf.toString();
byte[] img_byte = s.getBytes();

Now d problem I found is, when I send Bytes from Mob App, some bytes are LOST , whose value is 0A and 0D-Hex ...
Exactly, Cr- Carriage Return & Lf- Line Feed...

It means, POST method OR readLine() not able to accept 0A & 0D value...

And so I come to know that, LOST bytes are 0A and 0D occurrence in image's byte array....

Any one have any idea, how to do this, or how to use any another method.....
Thanks
-Akash
Alex Parvan
Ranch Hand

Joined: Dec 10, 2009
Posts: 115
I upload images to servlet using the following code:

Mobile:


Servlet:


This is something i use and works every time. I also looked and asked everywhere for this and was very hard to get, i hope this helps you.
Alex.


"Quoting yourself is stupid" - Me
akash kubavat
Greenhorn

Joined: May 14, 2010
Posts: 2
Thank you, thank you very much,..... Alex Parvan

You done it..
You resolve my problem, which was teasing me for last 1 week...


mahta moradzadeh
Greenhorn

Joined: Jan 13, 2009
Posts: 3
Hi all.
I used above code and created my image from data byte array :

while ((bytesRead = din.read(buffer)) > 0 ) {
// construct large enough array for all the data we now have
byte[] newData = new byte[data.length + bytesRead];
// copy data previously read
System.arraycopy(data, 0, newData, 0, data.length);
// append data newly read
System.arraycopy(buffer, 0, newData, data.length, bytesRead);
// discard the old array in favour of the new one
data = newData;

}

FileOutputStream f1;
File directory = new File("E:/Image/");
directory.mkdirs();
String path = "E:/Image/mayImage.jpg" ;

try {
f1 = new FileOutputStream(path);
f1.write(data);
f1.close();
} catch (IOException e) {
System.out.println("Problems creating the file");
}



the created image with name mayImage.jpg is not a valid image and its size is greater than original image size.
how you created your image form these data(maybe i do something wrong after reading data and writing in a file).
Thanks in advance!

mahta moradzadeh
Greenhorn

Joined: Jan 13, 2009
Posts: 3
I found my mistake, i was sending wrong data from midlet
Olga Flasza
Greenhorn

Joined: Mar 26, 2010
Posts: 28
Hi Alex, Thanks for your nice clarification.. do you have any code for php & mysql on the server side?
It has become quite hard for me trying to use php & mysql. I solved it somehow.. but as HTTP 1.1 requires data to be sent as 'chunk' from I need to create HTTP connection each time to send each 'chunk'. It would not be a problem if I don't need to ask the MIDlet user every time for internet connection, which is disturbing. Finally, I found out I need to sign the MIDlet to get rid of such situation.. and I need to buy some key or what. I still don't know much about it.
Any help is greatly appreciated
Rahul Bangar
Greenhorn

Joined: Jul 23, 2012
Posts: 1
How to download image from servlet in j2me??
 
GeeCON Prague 2014
 
subject: send image in post method from J2ME to Servlet