hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gerry Woods" <gerry.wo...@soa.com>
Subject RE: File upload without a file?
Date Sat, 16 Jun 2007 01:08:58 GMT
Have you tried using a MultipartRequestEntity with a StringPart on a
PostMethod?  That seems to be the non-deprecated way to do it.
Something like:

  PostMethod post = new PostMethod();
  post.setRequestEntity(new MultipartRequestEntity(new Part[]{ new
StringPart(name, data) }, new HttpMethodParams()));


-----Original Message-----
From: Anthony Zepezauer [mailto:Anthony.Zepezauer@cnet.com] 
Sent: Friday, June 15, 2007 5:54 PM
To: httpclient-user@jakarta.apache.org
Subject: File upload without a file?

Hi, I am new to HttpClient and new to the list. . . . I hope this isn't
too obvious of a question but I've searched the documentation and the
archives and haven't found an answer yet.

Suppose I have a String that I want to upload to a URL as a file.  The
way my last boss told me to do it was to write the String to disk,
upload it, then delete the disk copy, like this:

        MultipartPostMethod multipartPostMethod = new
MultipartPostMethod(uploadUrl);
        // write tempFile
        try {
            multipartPostMethod.addParameter("uploaded_file", filename,
tempFile);
            multipartPostMethod.addParameter("func", "upload");
            multipartPostMethod.addParameter("pwd", "");
            httpclient.executeMethod(multipartPostMethod);

        } finally {
            if (tempFile != null) tempFile.delete();
            multipartPostMethod.releaseConnection();
        }

Which works fine.  But, my new boss doesn't like the writing to disk,
for various reasons.  She wants me to upload the String directly without
writing it to disk first.  I found this in the documentation:

"The PartSource interface provides a generic container for providing
data to the FilePart class. . . The input for the multipart post could
come from anywhere, perhaps it's being received from another server or
process, and all that the PartSource class needs to be able to do is
provide the length of the data that will be provided, an input stream to
retrieve the data from and a file name (or some name identifying the
data)."

http://jakarta.apache.org/commons/httpclient/methods/multipartpost.html

So after digging into Part and FilePart, I tried this:

            byte[] bytes = "i am the STRING upload test".getBytes();

            PartSource partSource = new
ByteArrayPartSource("stringUploadTest", bytes);
            Part part = new FilePart("stringUploadTest", partSource);

            multipartPostMethod.addPart(part);
            multipartPostMethod.addParameter("func", "upload");
            multipartPostMethod.addParameter("pwd", "");
            httpclient.executeMethod(multipartPostMethod);

This doesn't work.  I don't get any errors, but no file arrives at the
destination.

Any idea what I'm doing wrong?  Am I on the right track at all?  Any
help is appreciated.


Thanks,
Anthony Z.

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message