import java.io.File;
 
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.HeadMethod;
+
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.app.Notification;
             mRemotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
         }
 
-        for (int i = 0; i < mRemotePaths.length; ++i)
-            mRemotePaths[i] = mRemotePaths[i].replace(' ', '+');
-
         if (mLocalPaths.length != mRemotePaths.length) {
             Log.e(TAG, "Remote paths and local paths are not equal!");
             return Service.START_NOT_STICKY;
         
         for (int i = 0; i < mLocalPaths.length; ++i) {
             
-            String mimeType;
+            String mimeType = null;
             try {
                 mimeType = MimeTypeMap.getSingleton()
                         .getMimeTypeFromExtension(
                                     .lastIndexOf('.') + 1));
             } catch (IndexOutOfBoundsException e) {
                 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mLocalPaths[i]);
-                mimeType = "application/octet-stream";
             }
+            if (mimeType == null)
+                mimeType = "application/octet-stream";
             
             mCurrentIndexUpload = i;
-            if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {
+            mRemotePaths[i] = getAvailableRemotePath(wc, mRemotePaths[i]);
+            if (mRemotePaths[i] != null && wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {
                 mSuccessCounter++;
                 OCFile new_file = new OCFile(mRemotePaths[i]);
                 new_file.setMimetype(mimeType);
         run();
     }
 
+    /**
+     * Checks if remotePath does not exist in the server and returns it, or adds a suffix to it in order to avoid the server
+     * file is overwritten.
+     * 
+     * @param string
+     * @return
+     */
+    private String getAvailableRemotePath(WebdavClient wc, String remotePath) {
+        Boolean check = wc.existsFile(remotePath);
+        if (check == null) {    // null means fail
+            return null;
+        } else if (!check) {
+            return remotePath;
+        }
+    
+        int pos = remotePath.lastIndexOf(".");
+        String suffix = "";
+        String extension = "";
+        if (pos >= 0) {
+            extension = remotePath.substring(pos+1);
+            remotePath = remotePath.substring(0, pos);
+        }
+        int count = 2;
+        while (check != null && check) {
+            suffix = " (" + count + ")";
+            if (pos >= 0)
+                check = wc.existsFile(remotePath + suffix + "." + extension);
+            else
+                check = wc.existsFile(remotePath + suffix);
+            count++;
+        }
+        if (check == null) {
+            return null;
+        } else if (pos >=0) {
+            return remotePath + suffix + "." + extension;
+        } else {
+            return remotePath + suffix;
+        }
+    }
+
     @Override
     public void transferProgress(long progressRate) {
         mSendData += progressRate;