Hi

Don't get confused
Used below pasted code


HttpURLConnection oHttpConnection = null;
        OutputStream oOutputStream = null;
        FileInputStream oFileInputStream = null;
        java.io.InputStream oInputStream = null;

        boolean bSuccess = true;
        byte[] arrBuf = null;

        try
        {
            File oCurrentFile = null;
           oCurrentFile = new File(oFileMetaData.m_strFile);


            // make server connection
            oHttpConnection = Connector.fnOpen(oFileMetaData.m_strURL,
Connector.POST);

            //open output stream
            oOutputStream = oHttpConnection.getOutputStream();

            //open fileInputStream
            oFileInputStream = new FileInputStream(oCurrentFile);

            // Max Buffer Size
            int iBufferSize = 1024;
            // Buffer
            arrBuf = new byte[iBufferSize];

            int nReadBytes = -1;

            /*
             * read chunk by chunk till
             * end of file and upload chunk-by-chunk
             * to server
             */
//                /*for debugging*/
//                /*===================================*/
//                int size = 0;
//                /*===================================*/
            while(-1 != (nReadBytes = oFileInputStream.read(arrBuf)))
            {
                    oOutputStream.write(arrBuf, 0, nReadBytes);

//                    /*for debugging*/
//                    /*===================================*/
//                    size+=nReadBytes;
//                    Logger.Log("chunk size:::"+ nReadBytes + ":::total
size:::" + size);
//                    /*===================================*/
            }
            oOutputStream.flush();
            oOutputStream.close();
            oFileInputStream.close();
            arrBuf = null;

            // get connection response code
            int nStatus = oHttpConnection.getResponseCode();

            switch (nStatus)
            {
                // 200 = Ok (Successful)
                case HttpURLConnection.HTTP_OK:
                {
                    // Read data from the input stream
                    oInputStream = oHttpConnection.getInputStream();

                    int nSize = oHttpConnection.getContentLength();

                    arrBuf = new byte[nSize];
                    int len = 0;

                    while(len < nSize)
                    {
                        arrBuf[len++] = (byte)oInputStream.read();
                    }

                    //cross check that all the data read properly from
server
                    if(len < nSize)
                        bSuccess = false;

                    break;
                }
                    // 400: The request could not be understood by the
server due to
                    // malformed syntax.
                case HttpURLConnection.HTTP_BAD_REQUEST:
                {
                    bSuccess = false;
                    break;
                }
                    // 401: The request requires user authentication.
                case HttpURLConnection.HTTP_UNAUTHORIZED:
                {
                    bSuccess = false;
                    break;
                }
                    // 403: The server understood the request, but is
refusing to
                    // fulfill it.
                case HttpURLConnection.HTTP_FORBIDDEN:
                {
                    bSuccess = false;
                    break;
                }
                    // 404: The server has not found anything matching the
                    // Request-URI.
                case HttpURLConnection.HTTP_NOT_FOUND:
                {
                    bSuccess = false;
                    break;
                }
                    // 502: The server, while acting as a gateway or proxy,
received
                    // an
                    // invalid response from the upstream server it accessed
in
                    // attempting
                    // to fulfill the request
                case HttpURLConnection.HTTP_BAD_GATEWAY:
                {
                    bSuccess = false;
                    break;
                }
                    // 503: The server is currently unable to handle the
request due
                    // to
                    // a temporary overloading or maintenance of the server
                case HttpURLConnection.HTTP_UNAVAILABLE:
                {
                    bSuccess = false;
                    break;
                }
            }
        } catch (Exception e)
        {
            bSuccess = false;
            Logger.Log("Exception in downloading File::: Donwloader UTIL:::"
+ e);
            throw new Exception("");
        } finally
        {
            try
            {
                // close inputStream
                if (null != oInputStream)
                {
                    try
                    {
                        oInputStream.close();
                    } catch (Exception e){}

                    oInputStream = null;
                }

                // close outputStream
                if (null != oOutputStream)
                {
                    try
                    {
                        oOutputStream.close();
                    } catch (Exception e){}

                    oOutputStream = null;
                }

                // close FileInputStream
                if (null != oFileInputStream)
                {
                    try
                    {
                        oFileInputStream.close();
                    } catch (Exception e){}

                    oFileInputStream = null;
                }
                // close http connection
                if (null != oHttpConnection)
                {
                    try
                    {
                        oHttpConnection.disconnect();
                    } catch (Exception e){}

                    oHttpConnection = null;
                }
            } catch (Exception ex) {
                bSuccess = false;
                Logger.Log("Exception in closing connection::: Donwloader
UTIL:::" + ex);
                throw new Exception("");
            }

On Tue, Aug 3, 2010 at 1:09 PM, Pedro Teixeira <[email protected]>wrote:

> Hey,
>
> I'm trying to upload a image file. Might be .jpg or .png to my server.
> I found some confusing tutorials which people are doing it in
> different ways.
> Is there any simple or at least clear tutorial for this?
>
> Thank you
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to