You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Sushil Sureka <su...@gmail.com> on 2005/03/31 22:46:29 UTC
Multi part upload
Before I ask my question, I just wanted to let you know that I have
been through FAQs and other places but I can't seem to get it right. I
have this piece of code to upload the file
File file = new File("c:\\temp.txt");
FilePart filePart = new FilePart("theFile", file);
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
filePart.send(os);
InputStream i = convert(os);
request.setUserData(i);
//request.setContentType("multipart/form-data");
i.close();
} catch (IOException e) {
System.out.println("Error Building Multipart");
}
public static InputStream convert( ByteArrayOutputStream os )
throws IOException
{
return new ByteArrayInputStream(os.toByteArray());
}
But when the app tried to parse the data on server side, it gets this exception
Failed to parse multipart request
org.apache.commons.fileupload.FileUploadException: the request was rejected beca
use no multipart boundary was found
I am using http client 2.0.2
Any help will be appreciated.
thanks
Sushil
Re: Multi part upload
Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Sushil,
In article <c8...@mail.gmail.com>,
Thu, 31 Mar 2005 14:46:29 -0600,
Sushil Sureka <su...@gmail.com> wrote:
sushil> Before I ask my question, I just wanted to let you know that I have
sushil> been through FAQs and other places but I can't seem to get it right. I
sushil> have this piece of code to upload the file
sushil>
sushil> File file = new File("c:\\temp.txt");
sushil> FilePart filePart = new FilePart("theFile", file);
sushil> try {
sushil> ByteArrayOutputStream os = new ByteArrayOutputStream();
sushil> filePart.send(os);
sushil> InputStream i = convert(os);
sushil> request.setUserData(i);
sushil> //request.setContentType("multipart/form-data");
sushil> i.close();
sushil> } catch (IOException e) {
sushil> System.out.println("Error Building Multipart");
sushil> }
sushil>
sushil> public static InputStream convert( ByteArrayOutputStream os )
sushil> throws IOException
sushil> {
sushil> return new ByteArrayInputStream(os.toByteArray());
sushil> }
sushil>
sushil> But when the app tried to parse the data on server side, it gets this exception
sushil> Failed to parse multipart request
sushil> org.apache.commons.fileupload.FileUploadException: the request was rejected beca
sushil> use no multipart boundary was found
You might refer RFC1867 'Form-based File Upload in HTML' to know
how file-upload works.
http://www.ietf.org/rfc/rfc1867.txt
The RFC shows an example of data sent back from client as follows:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-data; name="field1"
Joe Blow
--AaB03x
content-disposition: form-data; name="pics"; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
As this example shows, you need to specify a boundary string
in the Content-Type field for the HTTP request,
'Content-type: multipart/form-data, boundary=YOUR-OWN-BOUNDARY'
for example. Then, use the boundary string to separate and terminate
each part of the multi-part content.
Hope this helps,
----
Kazuhito SUGURI
mailto:suguri.kazuhito@lab.ntt.co.jp