Wednesday, September 22, 2010

Android File Upload

This is the code for upload the file from the android phone to php server.


public void upload(String fileName, String filePath, String strURL) {
        if (fileName == null || fileName.equals("")) {
            throw new RuntimeException("FileName cannot be empty");
        }
        if (filePath == null || filePath.equals("")) {
            throw new RuntimeException("FilePath cannot be empty");
        }
        if (strURL == null || strURL.equals("") && !TextValidation.isValidURL(strURL)) {
            throw new RuntimeException("Url cannot be empty");
        }
        // Get the file input stream
        FileInputStream fis = setFile(filePath);
       
        // indicator for the upload file white spaces
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        String exsistingFileName = fileName;
        try {
            // Http connection and parameter setting
            URL connectURL = new URL(strURL);
            HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
           
            // Data Stream for sending file
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\"" + exsistingFileName + "\"" + lineEnd);
            dos.writeBytes(lineEnd);
            int bytesAvailable = fis.available();
            int maxBufferSize = 1024 * 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bufferSize];
            int bytesRead = fis.read(buffer, 0, bufferSize);
           
            // output stream populate
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fis.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fis.read(buffer, 0, bufferSize);
            }
           
            // actual upload
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
           
            // close the input stream
            fis.close();
            // flush the data output stream
            dos.flush();
            // response received
            InputStream is = conn.getInputStream();
            int ch;
           
            StringBuffer b = new StringBuffer();
            while ((ch = is.read()) != -1) {
                b.append((char) ch);
            }
            Log.i("Response", ">" + b.toString());
        } catch (Exception ex) {
            Log.e("Exception", ">" + ex.toString());
            throw new RuntimeException("Exception: " + ex.toString());
        }
    }

No comments:

Post a Comment