Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2008, 04:35 PM
Member
 
Join Date: Dec 2008
Posts: 25
Rep Power: 0
angelicsign is an unknown quantity at this point
Default Receive uploaded image / video (byte array) from j2me on jsp.HOW???
currently i'm trying to do a project, it needs to send upload a video to the server and store inside a folder. But i found that it just able to receive a set of data that < 1024bytes, i done that by uploading an image. the code i found is like that.

/************************************************** ***************************

Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class PostFile extends MIDlet implements Runnable, CommandListener{
    
    private final String FILE = "/image.jpg";
    private final String URL = "url here"; // change this to a valit page.
    
    private final String CrLf = "\r\n";
    
    private Form form = null;
    private Gauge gauge = null;
    private Command exitCommand;
    private Command uploadCommand;
    
    public PostFile(){
        form = new Form("Upload File");
        gauge = new Gauge("Progress:", true, 100, 0);
        form.append(gauge);
        
        exitCommand = new Command("Exit", Command.EXIT, 0);
        uploadCommand = new Command("Upload", Command.SCREEN, 0);
        form.addCommand(exitCommand);
        form.addCommand(uploadCommand);
        
        form.setCommandListener(this);
    }
    
    public void startApp() {
        Display.getDisplay(this).setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    private void progress(int total, int current){
        int percent = (int) (100 * ((float)current/(float)total));
        gauge.setValue(percent);
    }
    
    public void run() {
        httpConn();
    }    
    
    private void httpConn(){
        HttpConnection conn = null;
        OutputStream os = null;
        InputStream is = null;
        
        try{
            System.out.println("url:" + URL);
            conn = (HttpConnection)Connector.open(URL);
            conn.setRequestMethod(HttpConnection.POST);

            String postData = "";

            InputStream imgIs = getClass().getResourceAsStream(FILE);
            byte []imgData = new byte[imgIs.available()];
            imgIs.read(imgData);

            String message1 = "";
            message1 += "-----------------------------4664151417711" + CrLf;
            message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + FILE + "\"" + CrLf;
            message1 += "Content-Type: image/jpeg" + CrLf;
            message1 += CrLf;

            // the image is sent between the messages ni the multipart message.

            String message2 = "";
            message2 += CrLf + "-----------------------------4664151417711--" + CrLf;               

            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
            // might not need to specify the content-length when sending chunked data.
            // conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length)));

            System.out.println("open os");
            os = conn.openOutputStream();

            System.out.println(message1);
            os.write(message1.getBytes());

            // SEND THE IMAGE
            int index = 0;
            int size = 1024;
            do{
                System.out.println("write:" + index);
                if((index+size)>imgData.length){
                    size = imgData.length - index; 
                }
                os.write(imgData, index, size);
                index+=size;
                progress(imgData.length, index); // update the progress bar.

            }while(index<imgData.length);
            System.out.println("written:" + index);            

            System.out.println(message2);
            os.write(message2.getBytes());
            os.flush();

            System.out.println("open is");
            is = conn.openInputStream();

            char buff = 512;
            int len;
            byte []data = new byte[buff];
            do{
                System.out.println("READ");
                len = is.read(data);

                if(len > 0){
                    System.out.println(new String(data, 0, len));
                }
            }while(len>0);

            System.out.println("DONE");

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            System.out.println("Close connection");
            try{
                os.close();
            }catch(Exception e){}
            try{
                is.close();
            }catch(Exception e){}
            try{
                conn.close();            
            }catch(Exception e){}
        }
    }    
    
    public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
        if(command == exitCommand){
            this.notifyDestroyed();
        }else if(command == uploadCommand){
            new Thread(this).start();
        }
    }    
}
from this code, how should my jsp coding be done to receive the data sent by j2me?
by modifying the code,i just manage to get a chunk of 1024bytes of a total of 42986bytes image.
can some body help me?
this is urgent for me.thanks...

Last edited by angelicsign; 12-02-2008 at 04:49 PM. Reason: change the coding to code tag
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
J2ME/Android Video Streaming Project mox27 Forum Lobby 4 04-06-2009 09:48 AM
Java code that allows me to make and receive calls, send and receive sms nareshbabu@live.in Networking 0 12-02-2008 11:55 AM
uploaded image not shown java_srinivasan JavaServer Pages (JSP) and JSTL 0 11-05-2008 01:40 PM
[SOLVED] Image to byte[] playwin2 New To Java 25 09-23-2008 06:50 PM
work on byte image in j2me sunitikumar CLDC and MIDP 0 07-24-2008 05:12 PM


All times are GMT +2. The time now is 06:17 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org