Trouble with method network.upload... HELP PLEASE!

how can i upload an image to a server?

I have a trouble with the method network.upload is that, when i need upload an image to a java server but this method puts a headers that corrupt the file and i cant work with the file, because I need the server to process the image, but is not received correctly. I work in java with sockets. Here is the example…

Lua client

local function networkListener( event )

         if ( event.isError ) then

                 print( “Network error!”)

         elseif ( event.phase == “ended” ) then

                print ( “Upload complete!” )

         end

 end

network.upload( 

         “http://localhost:9000”, 

          “POST”, 

         networkListener, 

         “test.jpg”, 

         system.DocumentsDirectory, 

         “image/jpg” 

        )

java server

public class Server implements Runnable {

    ServerSocket server;

    public Server() {

        try {

            server=new ServerSocket(9000);

            System.out.println(“Server running…”);

        } catch(Exception e) {

            System.out.println(“Error !\n”+e);

            System.exit(1);

        }

    }

    public void run() {

        Socket client=null;

        while (true) {

            if (server==null)

                return;

            try {

                client=server.accept();

                System.out.println(“get a client !”);

            } catch(java.io.IOException e) {

                System.err.println(“conection failed” + e.getMessage());

            }

            try {

                java.io.InputStream in = client.getInputStream();

                java.io.FileOutputStream out = new java.io.FileOutputStream(new java.io.File(“D:\test.jpg”));

                byte[] buf = new byte[1024];

                int len;

                while ((len = in.read(buf)) > 0) {

                    out.write(buf, 0, len);

                }

                in.close();

                out.close();

                server.close();

            } catch(java.io.IOException e) {

                System.out.println("Error: " + e);

            }

        }

    }

    public static void main(String a[]) {

        Server server = new Server();

        new Thread(server).start();

    }

}

original image file before sending

thump_8740595captura1.png

image file after sending

thump_8740594captura.png

How can i do to delete these headers?

Thanks a lot!

I think your best bet would be to ask this on a forum that handle server side Java programming.  While some Android native developers might spot something, I think you are going to need someone with more server side programming in Java than you’re likely to find here.  You could also consider asking on stackoverflow.com.

Rob

Hi Rob, 

in that way this may be wrong?.. in that case there are a different way to send the image file to a java server by Sockets?, because i need send the file from the lua client and i dont know what more to do, can you give me some choices?

Thanks!

I think the client is (most likely) sending the proper image data to the server.  As Rob said, the issue is really on the server-side, not the client side.

The header lines are part of a standard HTTP transaction.  Your client is supposed to send that information, and the server is supposed to interpret it and act accordingly (which yours isn’t).

Given it’s name, I’d guess that ServerSocket just opens a socket and transfers bytes to/from that socket to your program – it’s not a full HTTP server.  Those header lines are really there in the data-stream, so ServerSocket is correct in transferring them/printing them.

You might want to look for a more full-featured HTTP-server class to derive from instead.  Also as Rob mentioned, Stackoverflow is of use here:

     http://stackoverflow.com/questions/2717294/create-a-simple-http-server-with-java

network.upload (and network.request ) are by definition http services (docs say: “Makes an asynchronous HTTP or HTTPS request to a URL.”)

So there’s no way around the headers using Corona’s network.* calls. Like they say above, socket calls won’t include the http headers. However, you could probably just write a little java code to copy the image into a new buffer without the headers and just use that (or save the same buffer, just skipping past the http headers).

Might be a lot simpler to do that than change protocols at this point?

network.request() and it’s sibling calls assume you are talking to a web server that responds to standard HTTP requests (GET, POST, PUT, DELETE, HEAD).  These requests are text based and are things like GET /index.html.  Headers and things are passed in.  If you are just grabbing that stream we expect to receive certain responses back like a standard web server.   You need to do more than just strip out the headers we are sending. 

If you must do this on the Java side, there is probably a standard HTTP java server that you could extend as necessary. 

Rob

I think your best bet would be to ask this on a forum that handle server side Java programming.  While some Android native developers might spot something, I think you are going to need someone with more server side programming in Java than you’re likely to find here.  You could also consider asking on stackoverflow.com.

Rob

Hi Rob, 

in that way this may be wrong?.. in that case there are a different way to send the image file to a java server by Sockets?, because i need send the file from the lua client and i dont know what more to do, can you give me some choices?

Thanks!

I think the client is (most likely) sending the proper image data to the server.  As Rob said, the issue is really on the server-side, not the client side.

The header lines are part of a standard HTTP transaction.  Your client is supposed to send that information, and the server is supposed to interpret it and act accordingly (which yours isn’t).

Given it’s name, I’d guess that ServerSocket just opens a socket and transfers bytes to/from that socket to your program – it’s not a full HTTP server.  Those header lines are really there in the data-stream, so ServerSocket is correct in transferring them/printing them.

You might want to look for a more full-featured HTTP-server class to derive from instead.  Also as Rob mentioned, Stackoverflow is of use here:

     http://stackoverflow.com/questions/2717294/create-a-simple-http-server-with-java

network.upload (and network.request ) are by definition http services (docs say: “Makes an asynchronous HTTP or HTTPS request to a URL.”)

So there’s no way around the headers using Corona’s network.* calls. Like they say above, socket calls won’t include the http headers. However, you could probably just write a little java code to copy the image into a new buffer without the headers and just use that (or save the same buffer, just skipping past the http headers).

Might be a lot simpler to do that than change protocols at this point?

network.request() and it’s sibling calls assume you are talking to a web server that responds to standard HTTP requests (GET, POST, PUT, DELETE, HEAD).  These requests are text based and are things like GET /index.html.  Headers and things are passed in.  If you are just grabbing that stream we expect to receive certain responses back like a standard web server.   You need to do more than just strip out the headers we are sending. 

If you must do this on the Java side, there is probably a standard HTTP java server that you could extend as necessary. 

Rob