Awesome, thanks very much for the suggestion Gerry, you put me on the
right track. Here's what finally worked (after upgrading to 3.0.1), in
case anybody else needs to do this:
PostMethod postMethod = new PostMethod(url);
try {
byte[] bytes = "i am the STRING file upload
test".getBytes();
PartSource partSource = new
ByteArrayPartSource("destinationFileName", bytes);
Part filePart = new FilePart("uploaded_file", partSource);
Part[] parts = {
new StringPart("func", "upload"),
new StringPart("pwd", ""),
filePart
};
postMethod.setRequestEntity(new
MultipartRequestEntity(parts, postMethod.getParams()));
httpclient.executeMethod(postMethod);
} finally {
postMethod.releaseConnection();
}
Thanks again,
Anthony
> -----Original Message-----
> From: Gerry Woods [mailto:[EMAIL PROTECTED]
> Sent: Friday, June 15, 2007 6:09 PM
> To: HttpClient User Discussion
> Subject: RE: File upload without a file?
>
> 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:[EMAIL PROTECTED]
> Sent: Friday, June 15, 2007 5:54 PM
> To: [email protected]
> 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/multipart
> post.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: [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]