Image uploading to server in android
<p>I had some problem before which I solved. Thanks a lot to everyone who replied to the queries.</p>

<p>I am trying to upload image from my android application to server (Servelet). I have also emulated the SD Card which contains one jpeg image. </p>

<p>I have been able to make connection with the server and get some message back. But so far I have not been able to retrieve the image on the server. I get error msg on the server. I am using multipart option. The code are as follows. Plz have a look and let me know what wrong I am doing.</p>

<p><strong>ANDROID SIDE</strong></p>

<pre><code>     String path="http://10.0.2.2:8080/ImageLocalizer/Localize";
        String pathToFile="/sdcard/building.jpg";
        text1.setText(path);

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;
        try
        {
        FileInputStream fileInputStream = new FileInputStream(new File(pathToFile) );

        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs &amp; Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type",  "multipart/form-data");
        connection.setRequestProperty("FileName", "building.jpg");

        DataOutputStream outputStream =new DataOutputStream( connection.getOutputStream());
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead &gt; 0)
        {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
        text1.setText(path);

        BufferedReader in =new BufferedReader(new InputStreamReader( connection.getInputStream() ) );

        String response="",msg="";
        while ( (response = in.readLine()) != null ) {
            msg+=response;
        }
        text2.setText(msg);
    }        
    catch (Exception ex)
     {}

    }
</code></pre>

<p><strong>SERVELET SIDE</strong></p>

<pre><code>   doPost()
    {
            System.out.println("RUNNING");
             InputStream in = request.getInputStream();
                BufferedReader r = new BufferedReader(new InputStreamReader(in));
                StringBuffer buf = new StringBuffer();
                String line;

                PrintWriter out = response.getWriter();
                out.println("Image copied !!");
                out.close();
                try  
                {  
                        PrintWriter out1=response.getWriter();  
                        FileItemFactory factory = new DiskFileItemFactory();  
                        ServletFileUpload upload = new ServletFileUpload(factory);  
                        String s[]=new String[30];  
                        byte i=0;  
                        boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
                        if(isMultipart)  
                        {  
                            List items=upload.parseRequest(request);      
                            Iterator iterator=items.iterator();  
                            while(iterator.hasNext())  
                            {  
                                FileItem fitem=(FileItem)iterator.next();  
                                if(!fitem.isFormField())  
                                {  
                                    StringBuffer s2=new StringBuffer(fitem.getName());                    
                                    if(s2!=null &amp;&amp; s2.length()&gt;0)  
                                    {  
                                        File fl=new File("H:\\abcd.jpg");  
                                        fitem.write(fl);  
                                    }  
                                }  
                                if(fitem.isFormField())  
                                {  
                                    s[i]=fitem.getString();  
                                    i++;  
                                }  
                            }  

                        }  

                }catch(Exception e){e.printStackTrace();}  


    }
</code></pre>
