×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    uploading image in a server????

    I have taken example from this link to upload an image to server-

    http://www.java-forums.org/javaserve...e-jsp-how.html

    it reads the image in chunks but after that an error is displayed as
    java.io.IOException: error 10054 during TCP read

    I want to know the reason why is it so???
    Is there any problem with this code?

    Code:
    InputStream Imgis = getClass().getResourceAsStream(FILE);
    			byte[] imgData = new byte[Imgis.available()];
    			Imgis.read(imgData);
    The error that is displayed is-

    java.io.IOException: error 10054 during TCP read
    at com.sun.midp.io.j2me.socket.Protocol.nonBufferedRead(+7)
    at com.sun.midp.io.BufferedConnectionAdapter.readBytes(+39)
    at com.sun.midp.io.BaseInputStream.read(+39)
    at com.sun.midp.io.BaseInputStream.read(+10)
    at java.io.DataInputStream.read(+7)
    at com.sun.midp.io.j2me.http.Protocol.readLine(+4)
    at com.sun.midp.io.j2me.http.Protocol.readResponseMessage(+17)
    at com.sun.midp.io.j2me.http.Protocol.finishRequestGetResponseHeader(+39)
    at com.sun.midp.io.j2me.http.Protocol.sendRequest(+50)
    at com.sun.midp.io.j2me.http.Protocol.sendRequest(+6)
    at com.sun.midp.io.j2me.http.Protocol.openInputStream(+9)
    at com.sun.midp.io.ConnectionBaseAdapter.openDataInputStream(+8)
    at audio.HttpPOSTMIDlet.httpConn(+411)
    at audio.HttpPOSTMIDlet.run(+4)

    It does not give me the response msg before that the connection is closed...
    Plz plz plz plz plz help me...it will be greatly appreciated...
    please help me with this.
    is there anyone who can help me with this
    Thanks.
    Last edited by yatibawri; 2009-12-26 at 11:51.

  2. #2
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    88

    Re: uploading image in a server????

    Hello,
    Which kind of connection are u working with!? Web Services or HTTP connection?! This is not an error of the data reading (such as the code u put here). Could u please put here the code that opens and sends the data?!

    []'s

  3. #3
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    Re: uploading image in a server????

    Quote Originally Posted by thiagobrunoms View Post
    Hello,
    Which kind of connection are u working with!? Web Services or HTTP connection?! This is not an error of the data reading (such as the code u put here). Could u please put here the code that opens and sends the data?!

    []'s
    I m working with http connection
    My code is as follows:
    Code:
    private byte[] httpConn() {
    		HttpConnection conn = null;
    		OutputStream os = null;
    		InputStream is = null;
    		
    		int ch;
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		byte[] res = null;
    
    
    		try {
    			System.out.println("url:" + URL);
    			conn = (HttpConnection) Connector.open(URL,Connector.READ_WRITE);
    			conn.setRequestMethod(HttpConnection.POST);
    			
    			InputStream Imgis = getClass().getResourceAsStream(FILE);
    			byte[] imgData = new byte[Imgis.available()];
    			Imgis.read(imgData);
    
    
    			String message1 = "";
    			message1 +=  CrLf;
    			message1 += "--AaB03x" + CrLf;
    			message1 += "Content-Disposition: form-data; name='userfile'; filename='image.jpg'" + CrLf;
    			message1 += "Content-Type: image/jpeg" + CrLf;
    			message1 += "Content-transfer-encoding: base64" + CrLf;
    			message1 += CrLf;
    			String message2 = "";
    			message2 += CrLf + "--AaB03x--"+ CrLf;
    			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");
    	
    			long len=conn.getLength();
    			byte[] data = new byte[512];
    			is = conn.openInputStream();
    			do {
    				System.out.println("READ");
    				len = is.read(data);
    				if (len > 0) {
    					System.out.println(new String());
    				}
    			} while (len > 0);
    			os.write(data, 0, 512);
    		
    
    			System.out.println("DONE");
    
    	    }catch(IOException 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){}
    	    }
    		return res;
    		
    	   
    	}
    please help me with the solution...
    Thanks for replying..

  4. #4
    Regular Contributor
    Join Date
    Sep 2008
    Location
    Ahmedabad
    Posts
    254

    Re: uploading image in a server????

    Hi,

    First of all you will need a Server side code to handle the data accordingly. I hope that would be ready. Does this problem happen everytime you try to upload the image or sometime only. Where exactly does the problem occur?

    http://forums.sun.com/thread.jspa?threadID=5174917

    Otherwise, please try the code mentioned in the above link.

    Hope that help you out.
    Sunil
    Mobile Application Developer

  5. #5
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    Re: uploading image in a server????

    Quote Originally Posted by sunil_bhambani123 View Post
    Hi,

    First of all you will need a Server side code to handle the data accordingly. I hope that would be ready. Does this problem happen everytime you try to upload the image or sometime only. Where exactly does the problem occur?

    http://forums.sun.com/thread.jspa?threadID=5174917

    Otherwise, please try the code mentioned in the above link.

    Hope that help you out.
    ya,i do have a server side php code,which handles the image that is send to the server.
    this problem happens everytime,i dont understand why it is so?

    the code below is an interprrtation of the code that i have already mentioned in my last post.
    Code:
    			os.flush();
    			
    			System.out.println("open is");//from here it goes to the catch block without reading further
    			char buff=512;
    			int len=(int) conn.getLength();
    			byte[] data = new byte[buff];
    			is = conn.openDataInputStream();
    			do {
    				System.out.println("READ");
    				System.out.println("length is:" +len);
    				len = is.read(data);
    				if (len > 0) {
    					System.out.println(new String(data, 0, len));
    				}
    			} while (len > 0);
    			os.write(data, 0, 512);
    please help me with it....
    Thanks.

  6. #6
    Regular Contributor
    Join Date
    Sep 2008
    Location
    Ahmedabad
    Posts
    254

    Re: uploading image in a server????

    Hi,

    What are you trying to read there? The image uploading code is in the above try block I suppose. Just try to comment out that code and then try.
    Sunil
    Mobile Application Developer

  7. #7
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    Re: uploading image in a server????

    Quote Originally Posted by sunil_bhambani123 View Post
    Hi,

    What are you trying to read there? The image uploading code is in the above try block I suppose. Just try to comment out that code and then try.
    u r correct
    i have comment that and after that there is no error.
    But,the problem is that
    whenever an image is send to my server successfully a response msg should come but in my code it does not come what is the problem.
    plz help

  8. #8
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    Re: uploading image in a server????

    whenever i try to print the response code as:

    form.append("\nResponse Code returned during POST is " +
    ""+conn.getResponseCode());


    the error that comes is as follow:
    Code:
    java.io.IOException: error 10054 during TCP read 
    	at com.sun.midp.io.j2me.socket.Protocol.nonBufferedRead(+7)
    	at com.sun.midp.io.BufferedConnectionAdapter.readBytes(+39)
    	at com.sun.midp.io.BaseInputStream.read(+39)
    	at com.sun.midp.io.BaseInputStream.read(+10)
    	at java.io.DataInputStream.read(+7)
    	at com.sun.midp.io.j2me.http.Protocol.readLine(+4)
    	at com.sun.midp.io.j2me.http.Protocol.readResponseMessage(+17)
    	at com.sun.midp.io.j2me.http.Protocol.finishRequestGetResponseHeader(+39)
    	at com.sun.midp.io.j2me.http.Protocol.sendRequest(+50)
    	at com.sun.midp.io.j2me.http.Protocol.sendRequest(+6)
    	at com.sun.midp.io.j2me.http.Protocol.getResponseCode(+8)
    	at audio.HttpPOSTMIDlet.httpConn(+455)
    	at audio.HttpPOSTMIDlet.run(+4)

  9. #9
    Regular Contributor
    Join Date
    Sep 2008
    Location
    Ahmedabad
    Posts
    254

    Re: uploading image in a server????

    Hi,

    Have you tested it on the real device? Which emulator are you using? I think you should go through the below URL, it suggests some ways of overcoming that. Try that, may be something might click for you.

    http://forums.sun.com/thread.jspa?threadID=5292371
    Sunil
    Mobile Application Developer

  10. #10
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    Re: uploading image in a server????

    Quote Originally Posted by sunil_bhambani123 View Post
    Hi,

    Have you tested it on the real device? Which emulator are you using? I think you should go through the below URL, it suggests some ways of overcoming that. Try that, may be something might click for you.

    http://forums.sun.com/thread.jspa?threadID=5292371

    Thanks for your prompt reply
    But, sunil_bhambani123
    i am still facing the same problem.
    while i try to make the request to the server i am getting
    "Response Code returned during POST is 200" through conn.getResponseCode();
    which means connection is successful,but the image is not loaded,there is some problem with it.
    In my server side code,there is a response msg "Connection Successful" but i am not getting the response back to my device.

    Can you help me with it.
    Last edited by yatibawri; 2009-12-28 at 09:17.

  11. #11
    Regular Contributor
    Join Date
    Sep 2008
    Location
    Ahmedabad
    Posts
    254

    Re: uploading image in a server????

    Hi,

    I hope the link helps you out finally.

    http://chrisblunt.com/blog/2006/08/1...omment-page-1/
    Sunil
    Mobile Application Developer

  12. #12
    Regular Contributor
    Join Date
    Sep 2008
    Location
    Ahmedabad
    Posts
    254

    Re: uploading image in a server????

    Hi,

    You can also try the code from the below link. I have tested that code and it works absolutely fine. The server side code is in JSP/Servlet. Its upto you to modify that into PHP, but it works for me.

    http://www.hcilab.org/documents/tuto...TTP/index.html
    Sunil
    Mobile Application Developer

  13. #13
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    Re: uploading image in a server????

    Quote Originally Posted by sunil_bhambani123 View Post
    Hi,

    I hope the link helps you out finally.

    http://chrisblunt.com/blog/2006/08/1...omment-page-1/
    Hi,
    Thanks a lot for your help.
    Now i am getting a connection successful message from server to the device,
    but my problem is with the length it is taking length as -1
    It should not be -1.
    So,plz can u tell me the reason why is it taking so???
    Can anyone help me out with it...
    My code is as follows:
    Code:
    .....
    
    public void run() {
    		HttpConnection conn = null;
    		OutputStream os = null;
    		DataInputStream is = null;
    		 boolean ret = false;
    		
    		try {
    			System.out.println("url:" + URL);
    			conn = (HttpConnection) Connector.open(URL,Connector.READ_WRITE);
    			conn.setRequestMethod(HttpConnection.POST);
    			
    			byte[] imgData = FILE.getBytes();
    
    			String message1 = "";
    			message1 +=  CrLf;
    			message1 += "--AaB03x" + CrLf;
    			message1 += "Content-Disposition: form-data; name='userfile'; filename='image.jpg'";
    			message1 += "image.jpg";
    			message1 += "''" + CrLf;
    			message1 += "Content-Type: image/jpeg" + CrLf;
    			message1 += "Content-transfer-encoding: Base64" + CrLf;
    			message1 += CrLf;
    			
    			String message2 = "";
    			message2 += CrLf + "--AaB03x--"+ CrLf;
    			
    			System.out.println("open os");
    			ByteArrayOutputStream bos = new ByteArrayOutputStream();
    			System.out.println(message1);
    			bos.write(message1.getBytes());
    			bos.write(imgData);    
    			System.out.println(message2);// the image is byte array !!!
    			bos.write(message2.getBytes());
    			byte data[] = bos.toByteArray();
    			conn.setRequestProperty("Content-Length", "" + data.length);
    			os = conn.openOutputStream();
    
    			os.write(data);
    			os.close();
    			System.out.println("open is");
                is = conn.openDataInputStream();
                System.out.println("open");
                ret = processServerResponce(conn, is);
               
    	    }catch(IOException ioe)
            {
                ioe.printStackTrace();
            }
            finally
            {
                try{
                if(is != null) is.close();
                if(os != null) os.close();
                if(conn != null) conn.close();
                }
                catch(IOException ioe){}
            }
    	    if(ret == false)
            {
    	    	System.out.println(errorMsg);
            }
    	}
    
    	private boolean processServerResponce(HttpConnection conn, DataInputStream dis) throws IOException {
    	    errorMsg = null;
    		if(conn.getResponseCode() == HttpConnection.HTTP_OK)
            {
                int length = (int) conn.getLength();
                System.out.println("length = " + length);//length should not be -1 here
                String str;
                if(length != -1)
                {
                    System.out.println("reading data");
                    byte servletData[] = new byte[length];
                    dis.readFully(servletData);
                    System.out.println("Received data = " + servletData + "\nLength " + servletData.length);
                    str = servletData.toString();
                    System.out.println("str = " + str);
                }
                else
                {
                    ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
                    int ch;
                    while((ch = dis.read()) != -1)
                    {
                        bStrm.write(ch);
                    }
                    str = new String(bStrm.toByteArray());
                    bStrm.close();
                }
                response.setText(str);
                return true;
            }
            else
            {
                errorMsg = new String(conn.getResponseMessage());
                return false;
            }
    
        }
    ....
    Last edited by yatibawri; 2009-12-29 at 11:25.

  14. #14
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    Re: uploading image in a server????

    plz help me with the 1st reply...
    it will be of great help.

  15. #15
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    127

    Re: uploading image in a server????

    I am still stuck at the same problem that my image is not getting uploaded to the server.
    Now i am trying this content transfer encoding as base 64 for the image as:

    message1 += "Content-transfer-encoding: Base64" ;

    so i am right in doing the above line?

    private final String FILE = "/image.jpg";
    byte[] imgData = FILE.getBytes();
    How to convert this imgData into string....plz help

Similar Threads

  1. How to upload image on server using HTTP Post.....??
    By pranay_preet in forum Symbian Networking & Messaging (Closed)
    Replies: 11
    Last Post: 2012-02-15, 07:43
  2. Capturing and sending image to server
    By mahesh_aquarius in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2008-12-05, 14:09
  3. uploading image to the server
    By knawaz in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2008-10-11, 19:27
  4. reading image file from server ??
    By nitrajpal in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2006-01-30, 07:50

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×