Skip to main content

Sending an image bytearray from mobile to java http server

4 replies [Last post]
oldshoe
Offline
Joined: 2008-08-27
Points: 0

Hello...
My handymidlet shoots a foto, this is saved in a bytearray and drawn for displaying it on the phonescreen.

Now I want to send this array via http to a java http server for drawing it there and saving...
I got it to send and receive a string. Because of headerinformation...I had to filter the inputstream to find the string. (with bufferedreader and an added keyword)

How can i filter the byte array out of the inputstream?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15
Points: 0

Hi Oldshoe,

It sounds like you may be doing something a bit different than is needed, and thus causing more work for yourself.

On the phone you want to setup a HttpConnection using Connector.open(url);
You will also need to set the request type to POST, you will also want to set the content-type header property to "application/octet", and the content-length to the size of the bytearray
Then open from the HttpConnection object an outputstream, writing your byte array.

Then on the server once you have the connection from the device you will want to get the content-length from the headers, build an array of that length of bytes then open the inputstream and read the input into the byte array.

You can not do this as a GET type request as the length of a GET querystring is at most 2k, but is platform dependent based on the web/app server.

-Shawn

oldshoe
Offline
Joined: 2008-08-27
Points: 0

Hi...

Thank you very much for your help. This way to solve the problem really seems to be much more easier.

But I still have Problems.

So my phone is configured how you described.

I can't really get the content-length with the server. But I tested everything with a byte array length of 20.
With System.out.... I give out the testarray before sending and after receiving. They are different!!!! :(

Here is my Server-Code:

import java.net.Socket;
import java.net.ServerSocket;
import java.io.InputStream;
import java.io.PrintStream;

public class Server {

public static void main(String args[]) {
try {
ServerSocket server;
PrintStream os;
Socket connection;
InputStream is;
server = new ServerSocket(8081);
byte testData[]=new byte[20];

System.out.println("Waiting ...");
while (true) {
connection = server.accept();

is = connection.getInputStream();

is.read(aufnahmeData);

is.skip(is.available());

System.out.println("TestData: "+testData);

}

} catch (Exception e) { // every fault ...
System.out.println("Ouch!!!");
}
}

}

sfitzjava
Offline
Joined: 2003-06-15
Points: 0

Oh you are rolling your own socket server not using a Java http server.
You will need to look at the HTTP protocol spec and learn the rules of how HTTP is sent to the server.

OR.. just write a servlet and run it in jetty or tomcat. (much easier) :)

But here is a simple http server code I found that might help you.

http://www.devpapers.com/article/99
and another at:
http://fragments.turtlemeat.com/javawebserver.php

-Shawn

oldshoe
Offline
Joined: 2008-08-27
Points: 0