Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
sowjanya1919
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

sending image file to server

i want to edit the user account in which along with firstname,lastname i am getting an image url. Through the url i am downloading the image. Next i am changing the image by browsing my device folder. This change now i want to send to server. The server is accepting the image in the form of file and the remaining in String. Please tell me how to convet the image to file and send to server along with Strings . Its urgent .Please help me

Please use plain text.
Developer
simon_hain
Posts: 15,956
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: sending image file to server

i suggest that you search for "upload image", there a lots of resolved threads on this topic
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Trusted Contributor
sowjanya1919
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

Re: sending image file to server

I tried a lot. Actually i loading the image from server ,convert it to bytes and then uploading that byte date to server. As in this form only i am getting the code from serching. but on the server side the image has to identified as file.Because of that it is throwing error.wat to do. This is the code where i am downloading from server:

now the "buffer" i am sending through multipart posting

 

try {
                            HttpConnection  httpConnection = (HttpConnection) Connector.open(barcodeurl);
                               rc = httpConnection.getResponseCode();
                               if (rc != HttpConnection.HTTP_OK) {
                                  
                               }
                               InputStream httpInput = httpConnection.openInputStream();
                               InputStream inp = getClass().getResourceAsStream("Mystuff/accountsetting_over_over.png");
                                is = inp;
                                data = IOUtilities.streamToBytes(inp);
                                }catch (Exception e) {
                                    // TODO: handle exception
                                }
                            
                                
                                  EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length);
                                  Bitmap bitmapScaled = new Bitmap(75,75);
                                  Bitmap b = image.getBitmap();
                      //      byte[] buffer = IOUtilities.streamToBytes(is);
                          //  is.read(buffer);
                          
                        is = b.getClass().getResourceAsStream("Mystuff/accountsetting_over_over.png");
                        byte[] buffer = new byte[is.available()];
                        is.read(buffer);
                          

Please use plain text.
Trusted Contributor
sowjanya1919
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

Re: sending image file to server

Sorry that above code is wrong....

This the right one:

 int rc;
                         String    barcodeurl=accountDetail.userProfilePicture;
                            barcodeurl +=GetServerDetail.getConnectionString();
                        try {
                            HttpConnection  httpConnection = (HttpConnection) Connector.open(barcodeurl);
                               rc = httpConnection.getResponseCode();
                               if (rc != HttpConnection.HTTP_OK) {
                                  
                               }
                               InputStream httpInput = httpConnection.openInputStream();
                               InputStream inp = httpInput;
                                is = inp;
                                data = IOUtilities.streamToBytes(inp);
                                }catch (Exception e) {
                                    // TODO: handle exception
                                }
                                
                            byte[] buffer = IOUtilities.streamToBytes(is);

 

Please use plain text.
Developer
simon_hain
Posts: 15,956
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: sending image file to server

if you want to upload something you will most likely need POST, not GET.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Trusted Contributor
sowjanya1919
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

Re: sending image file to server

Ya i know. After this above code i am using "buffer" value to post the data as follows:

 

 public byte[] getBody() {
        ByteVector buff = new ByteVector();
        
        for (int i = 0; i < parameters.size(); i++) {
            RequestParam param = (RequestParam) parameters.elementAt(i);
            if (param instanceof BinaryRequestParam)
            {
                appendArray(buff, getBinaryHeader((BinaryRequestParam)param));
                appendArray(buff, ((BinaryRequestParam)param).getData());
                appendArray(buff, CRLF.getBytes());
            } else {
                appendArray(buff, postParameter(param.getName(), param.getValue()));
            }
        }
        appendArray(buff, closeBoundary().getBytes());
        return buff.getArray();
    }

 

//for string values//

==============

 private static String postParameterAsString(String name, String value) {
        String result = boundary() + CRLF;
        result = result + "Content-Disposition: form-data; name=\"" + name + "\"" + CRLF + CRLF;
        result = result + value + CRLF;
        return result;
    }

//for image//

==========

 private byte[] getBinaryHeader(BinaryRequestParam param) {
        String header = boundary() + CRLF;
        header += "Content-Disposition: form-data;  filename=\"" + param.getName() + "\"" + CRLF+ CRLF;
        header+= ((BinaryRequestParam)param).getData()+CRLF;
     //  header += "Content-Type: "+ param.getContentType() + CRLF + CRLF;
        return header.getBytes();
    }

 

//for sending image and data//

=========================

 
        try {
            String urlToSend = url;
            connection = (HttpConnection)Connector.open(urlToSend, Connector.READ_WRITE, true);
            connection.setRequestMethod(HttpConnection.POST);
            connection.setRequestProperty("Content-Type", MULTIPART_CONTENT_TYPE);
            connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
            outputStream = connection.openOutputStream();
            
            int offset = 0;
            int chunk = DATA_CHUNK_SIZE;
            int len = bytes.length;
            while (offset < len) {
                if (offset + chunk >= len) {
                    chunk = len - offset;
                }
                outputStream.write(bytes, offset, chunk);
                offset += chunk;
            }
            return getResponseAsSting(connection);

} finally {
            safelyCloseStream(outputStream);
            safelyCloseStream(connection);
            if (bytes != null) {
                bytes = null; // notify VM it can safely free the RAM
            }
        }

//for getting the result//

===================

 private static String getResponseAsSting(HttpConnection conn) throws IOException {
        String result = "";
        InputStream inputStream = null;
        try {
            inputStream = conn.openInputStream();
            int len;
            byte[] data = new byte[512];
            do {
                len = inputStream.read(data);
                if(len > 0){
                    result += new String(data, 0, len);
                }
            } while(len > 0);
        } catch (Exception e) {
            if (e instanceof IOException) {
                throw (IOException)e;
            }
        } finally {
            safelyCloseStream(inputStream);
        }
        return result;
    }

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: sending image file to server

I am not clear on what your problem is.  Can you please explain what you are asking, in some other way - perhaps you could describe the steps your program is going through and the problem you are having at each step.  For example, are you have a problem opening the connection or reading the data or what?

 

It will be useful I think to get the process 100% clear in your own head too.  I suspect you may be mixing up sending data in bytes and sending data as text (you call it a String).  If you are sending data as text, then it is converted to bytes and so you need to consider what encoding method is used to convert the characters to bytes.  If you didn't understand this, then I suspect the process will cause you problems.   

 

Here are some general points that might help:

 

a) If you have to send the data to the Server as a String, then you need to find out what form this Sting needs to take.  The Server will specify that.  So check the requirements of the Server you are sending it to.

 

b) Your code in the last Post is faulty.  You attempt to read from the same input stream twice, is and inp are the same.  You can't do that - just create a copy of the byte array. 

 

c) I hope the code we see is just a cut down version.  There appears to be me to be a load of error checking and tidying up of open streams that would normally be part of this process that you have not included. 

 

d) I presume that the Server you are sending the image to is not the same Server you are getting the image from. 

Please use plain text.