hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anthony Zepezauer" <Anthony.Zepeza...@cnet.com>
Subject File upload without a file?
Date Sat, 16 Jun 2007 00:54:23 GMT
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
        // write tempFile
        try {
            multipartPostMethod.addParameter("uploaded_file", filename,
            multipartPostMethod.addParameter("func", "upload");
            multipartPostMethod.addParameter("pwd", "");

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

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


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.addParameter("func", "upload");
            multipartPostMethod.addParameter("pwd", "");

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

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

Anthony Z.

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

View raw message