Não tendo encontrado nenhuma solução multi-plataforma em Windows XP, Windows 7 e Windows Server 2003, tomamos a abordagem de um cliente Java usando o Apache HTTP Client .
Compilando:
javac -cp "lib\httpclient-4.1.3.jar;lib\httpcore-4.1.4.jar;lib\httpmime-4.1.3.jar" HttpSubmitter.java
Usando:
java -cp lib\httpclient-4.1.3.jar;lib\httpmime-4.1.3.jar;lib\httpcore-4.1.4.jar;lib\httpcore-4.1.4.jar;lib\commons-logging-1.1.1.jar;lib\commons-codec-1.4.jar;. HttpSubmitter "Username" textfile1.txt textfile2.txt ... textfilen.txt
Fonte:
Este arquivo faz o upload de alguns arquivos para um servidor web, e a string retornada é uma URL que é aberta no Internet Explorer.
import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
public class HttpSubmitter {
public static void main(String[] args) throws Exception {
String username = null;
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
MultipartEntity mpEntity = new MultipartEntity();
int counter = 0;
for (String filename: args) {
// First parameter passed will be the username, the rest are files.
if (username == null) {
username = filename;
} else {
// Attach file contents to the message
File file = new File(filename);
ContentBody cbFile = new FileBody(file, "text/plain");
mpEntity.addPart("userfile" + ++counter, cbFile);
}
}
ContentBody cbUsername = new StringBody(username);
mpEntity.addPart("username", cbUsername);
String url = "http://example.com/endpoint";
HttpPost httppost = new HttpPost(url);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
// Returned string will be a URL, we pass it as a url to internet explorer
// Please keep in mind the security implications of parsing a parameter you don't control to internet explorer.
String confirmation_page = EntityUtils.toString(resEntity);
String ie = "\"C:\Program Files\Internet Explorer\iexplore.exe\" " + confirmation_page;
// Runtime.getRuntime().exec(ie);
System.out.println(confirmation_page);
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
}