• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

send image in post method from J2ME to Servlet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 115
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
akash kubavat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, thank you very much,..... Alex Parvan

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


 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found my mistake, i was sending wrong data from midlet
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to download image from servlet in j2me??
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic