Jakarta HTTPClient upload problems
Hello,
After realising Sun's own Java HTTP methods insist on buffering
everything in memory before uploading to the server (which landed me
with OutOfMemory exceptions) I started to experiment with the Jakarta
alternative. So I tried this code:
= = = = = = = = = = = = =
String source = "file.zip";
String destination = "http://somewhere.com/perl/db.pl";
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(destination);
method.setRequestHeader("User-Agent", "ascii");
Part[] parts = {
new StringPart("action", "uploadfile"),
new StringPart("project", "lib"),
new FilePart(source, new File(source))
};
method.setRequestEntity(new MultipartRequestEntity(parts,
method.getParams()));
// Authenticate
client.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials("login",
"passwd");
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
// Execute the POST method
int statusCode = client.executeMethod(method);
if( statusCode != -1 )
{
String contents = method.getResponseBodyAsString();
method.releaseConnection();
System.out.println( contents );
}
}
catch(Exception e)
{
e.printStackTrace();
}
= = = = = = = = = = = = =
This will parse the first parameters ("action" & "project")
alright, but the code on the server which is to validate the parameters
stumble over the FilePart parameter, which it take to be an argument
called "file.zip". But even if I remove this piece of
validation-code the server fails at a later time since it cannot find
the Filehandle in the Perl CGI library. All this works fine with the
Java library.
I have tried some alternative ways to pass the parameters. Like:
= = = = = = = = = = = = =
// Configure the form parameters
method.addParameter("project", "lib");
method.addParameter("action", "uploadfile");
= = = = = = = = = = = = =
But it doesn't seem to be possible to pass Files with the addParameter
method. And:
= = = = = = = = = = = = =
NameValuePair[] data = {
new NameValuePair("project", "lib"),
new NameValuePair("action", "uploadfile"),
};
filePost.setRequestBody(data);
= = = = = = = = = = = = =
Doesn't take Files either. And is deprecated too I think.
any help? What am I doing wrong with this code to get Jakarta to do
upload of files? Or alternative, has Sun done anything so it is
possible to upload files without having everything stored in memory?
Thanks
/Rune