Index: gli/trunk/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java
===================================================================
--- gli/trunk/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java	(revision 16082)
+++ gli/trunk/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java	(revision 16098)
@@ -161,4 +161,16 @@
     }
 
+    static public int getGreenstoneVersion()
+    {
+	// returns message "Greenstone version is: <version number of the Greenstone remote server>"
+	String result = performAction(new RemoteGreenstoneServerVersionAction());
+	int index = result.indexOf(":") + 1; // includes space after colon 
+	result = result.substring(index);
+	System.err.println("***** version: " + result);
+	int greenstoneVersion = Integer.parseInt(result);
+	return greenstoneVersion;
+    }
+
+
     // ----------------------------------------------------------------------------------------------------
 
@@ -544,4 +556,20 @@
     }
 
+     /**
+     * --------------------------------------------------------------------------------------------
+     *    DISCOVERING WHAT VERSION THE REMOTE GREENSTONE SERVER IS (2 or 3)
+     * --------------------------------------------------------------------------------------------
+     */
+
+    static private class RemoteGreenstoneServerVersionAction
+	extends RemoteGreenstoneServerAction
+    {
+	public void perform()
+	    throws Exception
+	{
+	    String greenstone_version_command = "cmd=greenstone-server-version";
+	    action_output = sendCommandToServer(greenstone_version_command, null);
+	}	
+    }
 
     /**
@@ -1238,4 +1266,7 @@
 	throws Exception
     {
+	System.err.println("gliserver URL: " + upload_cgi);
+	System.err.println("gliserver args: " + cgi_args);
+
 	//For a remote GS3
 	//GS3 is running on Tomcat, and Tomcat requires a connection timeout to be set up at the client
@@ -1243,65 +1274,59 @@
 	//from Jakarta is applied to solve this problem only for uploading files. 
 	if (Gatherer.GS3){
-	    System.err.println("gliserver URL: " + upload_cgi);
-	    System.err.println("gliserver args: " + cgi_args);
-
-	// Setup the POST method
-	PostMethod httppost = new PostMethod(upload_cgi+"?"+cgi_args);
-
-	//read the zip file into a byte array
-	InputStream in = new FileInputStream (file_path);
-	File f = new File (file_path); // in order to get the length of the bytes array
-	ByteArrayOutputStream out = new ByteArrayOutputStream ((int)f.length());
-	int r = 0; // amount read
-	byte[] buf = new byte[1024];
-	while((r = in.read(buf, 0, buf.length)) != -1) {
-	    out.write(buf, 0, r);
-	}
-	in.close();
-	byte[] result = out.toByteArray();
-
-	// construct the multipartrequest form
-	PartSource partSource = new ByteArrayPartSource("zipFile", result);
-        FilePart filePart = new FilePart("uploaded_file", partSource);
-
-	String[] cgi_array=cgi_args.split("&");// get parameter-value paires from cgi_args string
-	Part[] parts=new Part[cgi_array.length+5];
-	parts[0]=filePart;
-	parts[1]= new StringPart("un", remote_greenstone_server_authentication.getUserName());
-	parts[2]= new StringPart("pw", new String(remote_greenstone_server_authentication.getPassword()));
-	parts[3]= new StringPart("ts", String.valueOf(System.currentTimeMillis()));
-	parts[4]= new StringPart("site", Configuration.site_name);
-	// find all parameters of cgi-agrs and add them into Part[]
-	for (int i=0; i<cgi_array.length;i++){
-	    parts[5+i]=new StringPart(cgi_array[i].substring(0,cgi_array[i].indexOf("=")),cgi_array[i].substring(cgi_array[i].indexOf("=")+1,cgi_array[i].length()));
-	}
-	// set MutilartRequestEntity on the POST method
-        httppost.setRequestEntity(new MultipartRequestEntity(parts, httppost.getParams()));
-	//set up the HttpClient connection
-	HttpClient client=new HttpClient();	
-        client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
-	client.getParams().setConnectionManagerTimeout(200000); //set the connection timeout
-	// get the output of the command from the server
- 	client.executeMethod(httppost);
-	String command_output = ""; 
-	try{
-	    client.executeMethod(httppost);
-	    if (httppost.getStatusCode() == HttpStatus.SC_OK) {
-		command_output = httppost.getStatusLine().toString();
-	    } else {
-		command_output = httppost.getStatusLine().toString();
-		System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
-	    }
-	}catch(IOException e){
-	    e.printStackTrace();
-	}finally{
-	    httppost.releaseConnection();
-	}
+	    // Setup the POST method
+	    PostMethod httppost = new PostMethod(upload_cgi);
+		    
+	    // construct the multipartrequest form
+	    String[] cgi_array=cgi_args.split("&");// get parameter-value pairs from cgi_args string
+	    Part[] parts=new Part[cgi_array.length+6];
+	    
+	    // We insert a dummy value for the first Part. For some reason, the gliserver side doesn't 
+	    // read the first Part (whether it's the file or a CGI argument like the cmd=upload-collection-file)
+	    parts[0]= new StringPart("dummy_arg", "dummy_value");  // unread dummy value
+	    
+	    // find all parameters of cgi-args and add them into Part[]
+	    for (int i=0; i<cgi_array.length;i++) {
+		parts[i+1]=new StringPart(cgi_array[i].substring(0,cgi_array[i].indexOf("=")),cgi_array[i].substring(cgi_array[i].indexOf("=")+1,cgi_array[i].length()));
+	    }
+	    parts[cgi_array.length+1]= new StringPart("un", remote_greenstone_server_authentication.getUserName());
+	    parts[cgi_array.length+2]= new StringPart("pw", new String(remote_greenstone_server_authentication.getPassword()));
+	    parts[cgi_array.length+3]= new StringPart("ts", String.valueOf(System.currentTimeMillis()));
+	    parts[cgi_array.length+4]= new StringPart("site", Configuration.site_name);
+	    // The FilePart: consisting of the (cgi-arg) name and (optional) filename in the Content-Disposition 
+	    // of the POST request Header (see CGI.pm), and the actual zip file itself. It uses the defaults:
+	    // Content-Type: application/octet-stream; charset=ISO-8859-1,Content-Transfer-Encoding: binary
+	    parts[cgi_array.length+5]= new FilePart("uploaded_file", "zipFile", new File(file_path));
+	    
+	    // set MultipartRequestEntity on the POST method
+	    httppost.setRequestEntity(new MultipartRequestEntity(parts, httppost.getParams()));
+
+	    // See file gli/request.txt for the multipart request that's been generated:
+	    //httppost.getRequestEntity().writeRequest(new FileOutputStream("request.txt", true)); // true: appends
+	    
+
+	    //set up the HttpClient connection
+	    HttpClient client=new HttpClient();	
+	    client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
+	    client.getParams().setConnectionManagerTimeout(200000); //set the connection timeout
+	    
+	    // get the output of the command from the server
+	    String command_output = ""; 
+	    try{
+		client.executeMethod(httppost);
+		if (httppost.getStatusCode() == HttpStatus.SC_OK) {
+		    command_output = httppost.getStatusLine().toString();
+		} else {
+		    command_output = httppost.getStatusLine().toString();
+		    System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
+		}
+	    }catch(IOException e){
+		e.printStackTrace();
+	    }finally{
+		httppost.releaseConnection();
+	    }
 	return command_output;
 	}
 
 	//For a remote GS2
-	System.err.println("gliserver URL: " + upload_cgi);
-	System.err.println("gliserver args: " + cgi_args);
 	// Add username and password, and a timestamp
 	cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName();
