Jakarta HTTPClient upload problems

W

WinstonSmith_101

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top