You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Giovannini Andrea <An...@formula.it> on 2004/04/06 19:37:06 UTC

NEWBIE FileUpload question

Hi,
I'm using FileUpload to process a file uploaded from Internet Explorer
via Jscript, there's no direct form submit but I use the XMLHTTP object
since I want control over the result. This is my client code

function go() {
    // I skip some details
    var fileName = ...;
    var url = ... + "&fileName=" + fileName;

    var adoStream = new ActiveXObject("ADODB.Stream");
    adoStream.Mode = 3;
    adoStream.Type = 1;
    adoStream.Open();
    adoStream.LoadFromFile(fileName);
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
    xmlhttp.open("POST", url, false);
    
    var boundary = "----------This_Is_The_Boundary_\r\n";
    xmlhttp.setRequestHeader("Content-Type","multipart/form-data;
boundary=" + boundary);
    xmlhttp.setRequestHeader("Content-Length", adoStream.Size);
    xmlhttp.send(adoStream.Read(adoStream.Size));
}

Then I want to save the file on the server. In my servlet I have this
code:

String path = ...
DiskFileUpload upload = new DiskFileUpload();
upload.setRepositoryPath(path);

try {
	List items = upload.parseRequest(request);
	Iterator iter = items.iterator();
	while (iter.hasNext()) {
		FileItem item = (FileItem) iter.next();
		if (!item.isFormField()) {
			fileName = item.getName();
			File uploadedFile = new File(fileName);
			item.write(uploadedFile);
		}
	}
} catch(Exception e) {
	...
}

But the parseRequest() returns an empty list. I've debugged the
FileUpload code and the problem is that in the discardBodyData() of the
class MultipartStream a MalformedStreamException("Stream ended
unexpectedly") is thrown and parseRequest() returns an empty collection.
So I wonder what's wrong with my uploading... Any idea?

Thanks in advance,
Andrea

----------------------------------
Andrea Giovannini
Java Software Architect

Gruppo Formula S.p.A.
----------------------------------

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


Re: NEWBIE FileUpload question

Posted by Martin Cooper <ma...@apache.org>.
I've been thinking about the problems you're having, and it seems to me that
you may be using the wrong tools for the job on both ends of the connection.
;-)

>From a brief Google around, it seems that (not surprisingly) XMLHTTP is
intended for processing (getting and posting) XML content. However, your
content appears to be CSV (comma separated values) data, rather than XML. In
addition, you appear to be constructing a request that contains only a
single "part", that being the content of a single file.

If what you really need to do is upload a single file and save it to disk on
the server, here's what I would suggest.

* On the client side, you don't really need XMLHTTP. However, if that is the
most convenient way of posting content, then it will still work for this
purpose.

* Do not set the content type header for multipart data. Instead, treat it
as a plain post.

* On the server side, don't use Commons FileUpload at all. Since you don't
need multipart handling, you don't need FileUpload either.

* Instead, just call ServletRequest.getInputStream() to get an input stream
for the body of the request - which is the contents of the file you posted -
and copy the contents of that stream to a file on the disk.

That seems to me to be a simpler way of doing what you need, but of course I
could be misunderstanding what you really need to do. ;-)

--
Martin Cooper


"Giovannini Andrea" <An...@formula.it> wrote in message
news:192D0B4A713A704895EACB84234B62653AA75C@hulk2000.gformula.net...
Hi,
I'm using FileUpload to process a file uploaded from Internet Explorer
via Jscript, there's no direct form submit but I use the XMLHTTP object
since I want control over the result. This is my client code

function go() {
    // I skip some details
    var fileName = ...;
    var url = ... + "&fileName=" + fileName;

    var adoStream = new ActiveXObject("ADODB.Stream");
    adoStream.Mode = 3;
    adoStream.Type = 1;
    adoStream.Open();
    adoStream.LoadFromFile(fileName);
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
    xmlhttp.open("POST", url, false);

    var boundary = "----------This_Is_The_Boundary_\r\n";
    xmlhttp.setRequestHeader("Content-Type","multipart/form-data;
boundary=" + boundary);
    xmlhttp.setRequestHeader("Content-Length", adoStream.Size);
    xmlhttp.send(adoStream.Read(adoStream.Size));
}

Then I want to save the file on the server. In my servlet I have this
code:

String path = ...
DiskFileUpload upload = new DiskFileUpload();
upload.setRepositoryPath(path);

try {
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
fileName = item.getName();
File uploadedFile = new File(fileName);
item.write(uploadedFile);
}
}
} catch(Exception e) {
...
}

But the parseRequest() returns an empty list. I've debugged the
FileUpload code and the problem is that in the discardBodyData() of the
class MultipartStream a MalformedStreamException("Stream ended
unexpectedly") is thrown and parseRequest() returns an empty collection.
So I wonder what's wrong with my uploading... Any idea?

Thanks in advance,
Andrea

----------------------------------
Andrea Giovannini
Java Software Architect

Gruppo Formula S.p.A.
----------------------------------




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


Re: NEWBIE FileUpload question

Posted by Martin Cooper <ma...@apache.org>.
I am not at all familiar with the XMLHTTP code you are using to create the
request, but I'm guessing that the problem may lie in the structure of the
request itself. If you could capture the raw request that is being submitted
(e.g. using Windows Network Monitor or Ethereal), that would help a lot.

--
Martin Cooper


"Giovannini Andrea" <An...@formula.it> wrote in message
news:192D0B4A713A704895EACB84234B62653AA75C@hulk2000.gformula.net...
Hi,
I'm using FileUpload to process a file uploaded from Internet Explorer
via Jscript, there's no direct form submit but I use the XMLHTTP object
since I want control over the result. This is my client code

function go() {
    // I skip some details
    var fileName = ...;
    var url = ... + "&fileName=" + fileName;

    var adoStream = new ActiveXObject("ADODB.Stream");
    adoStream.Mode = 3;
    adoStream.Type = 1;
    adoStream.Open();
    adoStream.LoadFromFile(fileName);
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
    xmlhttp.open("POST", url, false);

    var boundary = "----------This_Is_The_Boundary_\r\n";
    xmlhttp.setRequestHeader("Content-Type","multipart/form-data;
boundary=" + boundary);
    xmlhttp.setRequestHeader("Content-Length", adoStream.Size);
    xmlhttp.send(adoStream.Read(adoStream.Size));
}

Then I want to save the file on the server. In my servlet I have this
code:

String path = ...
DiskFileUpload upload = new DiskFileUpload();
upload.setRepositoryPath(path);

try {
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
fileName = item.getName();
File uploadedFile = new File(fileName);
item.write(uploadedFile);
}
}
} catch(Exception e) {
...
}

But the parseRequest() returns an empty list. I've debugged the
FileUpload code and the problem is that in the discardBodyData() of the
class MultipartStream a MalformedStreamException("Stream ended
unexpectedly") is thrown and parseRequest() returns an empty collection.
So I wonder what's wrong with my uploading... Any idea?

Thanks in advance,
Andrea

----------------------------------
Andrea Giovannini
Java Software Architect

Gruppo Formula S.p.A.
----------------------------------




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