You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Andrey Razumovsky <ra...@gmail.com> on 2009/04/06 17:47:29 UTC

Cannot read httpservlet's inputstream

Hi list,

I'm pretty sure I'm missing something obvious, but I just can't understand
what the root of problem is.
I try to send file from java to Tomcat6's http servlet. I need to send file
alone (and probably some parameters in request), so I don't wanna mess with
multipart libraries (commons-fileupload, httpclient). The problem is that I
can't read data from servlet's input stream - it is always empty. Here's my
code:

/////////// ON CLIENT SIDE
        URL url = new URL(address);
        HttpURLConnection connect = (HttpURLConnection)
url.openConnection();
        connect.setRequestMethod("POST");
        connect.setDoInput(true);
        connect.setDoOutput(true);
        connect.setAllowUserInteraction(false);
        connect.setUseCaches(false);
        //connect.setRequestProperty("Content-Length", "" + pack.length());
        //connect.setRequestProperty("Content-Type", "text/plain");

        OutputStream out = connect.getOutputStream();
        FileInputStream in = new FileInputStream(pack);

        IOUtils.copy(in, out);
        out.flush();
        out.close();
        in.close();

        if (connect.getResponseCode() != HttpURLConnection.HTTP_OK) {
            logObj.warn("file " + pack.getPath() + " was not transfered -
response code " +
                    connect.getResponseCode());
            return false;
        }

        logObj.info("file " + pack.getPath() + " transfered");

////////////// ON SERVER SIDE
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        logObj.info("Servlet started")
        logObj.info(IOUtils.toString(req.getInputStream()));
}

I see "file transfered" on client side, but nothing appears in log on server
side...

Thanks in advance,
Andrey

Re: Cannot read httpservlet's inputstream

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrey,

On 4/6/2009 11:47 AM, Andrey Razumovsky wrote:
>         //connect.setRequestProperty("Content-Type", "text/plain");

One more thing: you should /definitely/ set the charset in the Content-Type.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAknbk4EACgkQ9CaO5/Lv0PBPggCdFmUaClE4fxA/mMiNW6f9iI7B
ea4An3Y6t0gpxZtYmPJV/Ik+TVk60LV3
=I/pY
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrey,

On 4/6/2009 11:47 AM, Andrey Razumovsky wrote:
>         if (connect.getResponseCode() != HttpURLConnection.HTTP_OK) {
>             logObj.warn("file " + pack.getPath() + " was not transfered -
> response code " +
>                     connect.getResponseCode());
>             return false;
>         }

What response code do you get, here? 200?

> ////////////// ON SERVER SIDE
> protected void doPost(HttpServletRequest req, HttpServletResponse resp)
>         throws ServletException, IOException {
>         logObj.info("Servlet started")
>         logObj.info(IOUtils.toString(req.getInputStream()));
> }

Try turning-on the RequestDumperValve to see what the server is receiving.

http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html#Request%20Dumper%20Valve

Perhaps your servlet isn't configured to run on the URL pattern you are
using to POST?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAknac4EACgkQ9CaO5/Lv0PDcowCgqNsPzX5UFxIsgMYPoXN3cmPe
ij8An3JJzJOZ8OLPdhKSpS7+G6TPr/gn
=4uR5
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Mark Thomas <ma...@apache.org>.
André Warnier wrote:
> Mark Thomas wrote:
>> André Warnier wrote:
>>> Despite what you may wish and what you may have found in Google, I
>>> believe that if you want to send a file, you have to do it with a
>>> "multipart/form-data" content type.
>>
>> Nope. That is just plain wrong.
>>
>>> http://www.faqs.org/rfcs/rfc2616.html
>>> Sections 9.5 (POST) and 9.6 (PUT) describe the two methods that allow
>>> you to upload a file.  But 9.6 is not applicable, since you are sending
>>> this to a webapp, not directly to a URI.
>>
>> Wrong again. PUT is disabled by default for security reasons but it is
>> valid and is supported by the default servlet.
>>
> Ok Mark, I don't pretend I know how it works in Tomcat.
> How does one upload a file then ?
> 
> I mean other than with a PUT, as I presume that the OP doesn't just want
> to write files arbitrarily in his URI space on the server)

Put the content in the body of a POST, read the request body content in
your servlet and do whatever you need to do with it. If you want to add
extra attributes and send them via POST then you'll need to use
"multipart/form-data". At that point using a library will be easier than
coding it yourself although you can if you want. There are other ways of
sending extra attributes that don't use multipart.

There are, as usual, many ways of doing this and no one way is the best.
It will depend on what you are trying to do and why.

Mark



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
"/*" works! Thanks!

2009/4/9 Mark Thomas <ma...@apache.org>

> Andrey Razumovsky wrote:
> > Finally I did it! What was to be done is to change servlet mapping
> >
> > <url-pattern>/</url-pattern>
> > to
> > <url-pattern>/DoveServlet</url-pattern>
>
> Did you try:
> <url-pattern>/*</url-pattern>
> ?
>
> Mark
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Cannot read httpservlet's inputstream

Posted by Mark Thomas <ma...@apache.org>.
Andrey Razumovsky wrote:
> Finally I did it! What was to be done is to change servlet mapping
> 
> <url-pattern>/</url-pattern>
> to
> <url-pattern>/DoveServlet</url-pattern>

Did you try:
<url-pattern>/*</url-pattern>
?

Mark



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
Finally I did it! What was to be done is to change servlet mapping

<url-pattern>/</url-pattern>
to
<url-pattern>/DoveServlet</url-pattern>

so somewhy POST to servlet mapped to all urls does not work. Now I need URL
like /Dove/DoveServlet instead-of /Dove, but I'll survive this :)

2009/4/9 Andrey Razumovsky <ra...@gmail.com>

> Same evil happens when I try to send Multipart request:
>         HttpClient client = new DefaultHttpClient();
>         HttpPost post = new HttpPost(address);
>
>         MultipartEntity entity = new MultipartEntity();
>         entity.addPart("file", new FileBody(pack));
>         post.setEntity(entity);
>
>         HttpResponse response = client.execute(post);
>
>         logObj.info(response.getStatusLine());
>         if (response.getStatusLine().getStatusCode() !=
> HttpURLConnection.HTTP_OK) {
>             logObj.warn("file " + pack.getPath() + " was not transfered -
> response code " +
>                     response.getStatusLine().getStatusCode());
>         }
>
> I just can't understand why other POSTs work... Looks like something's
> wrong with my servlet
>
> 2009/4/9 Andrey Razumovsky <ra...@gmail.com>
>
> Hi friends,
>>
>> Problem still exists... Unforntunately I do not have a public URL. Could
>> you share you HTTP POST request code.
>> Lines with Content-Type, Content-Length etc are commented out because I
>> tried them but they didn't help. Event if I set them, server receives GET
>> with content-length=-1. Changing lines order and playing with header
>> properties gave no result. And once again, my problem is NOT that Tomcat
>> doesn't get POST body. It doesn't get POST at all!
>> Will try multipart requests with HTTPClient, hope it'll help...
>>
>> Andrey
>>
>> 2009/4/8 André Warnier <aw...@ice-sa.com>
>>
>> André Warnier wrote:
>>> Trying to redeem myself to Andrey for hijacking his post..
>>>
>>> Andrey, in your (latest) client code you do not set either a
>>> content-length, nor a "chunked" encoding headers.
>>> Is it possible that Tomcat 6 just ignores your POST content in that case
>>> ?
>>> In RFC2616, I find this in section 4.3 :
>>>  The presence of a message-body in a request is signaled by the
>>>   inclusion of a Content-Length or Transfer-Encoding header field in
>>>   the request's message-headers. A message-body MUST NOT be included in
>>>   a request if the specification of the request method (section 5.1.1)
>>>   does not allow sending an entity-body in requests. A server SHOULD
>>>   read and forward a message-body on any request; if the request method
>>>   does not include defined semantics for an entity-body, then the
>>>   message-body SHOULD be ignored when handling the request.
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>>
>

Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
Same evil happens when I try to send Multipart request:
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(address);

        MultipartEntity entity = new MultipartEntity();
        entity.addPart("file", new FileBody(pack));
        post.setEntity(entity);

        HttpResponse response = client.execute(post);

        logObj.info(response.getStatusLine());
        if (response.getStatusLine().getStatusCode() !=
HttpURLConnection.HTTP_OK) {
            logObj.warn("file " + pack.getPath() + " was not transfered -
response code " +
                    response.getStatusLine().getStatusCode());
        }

I just can't understand why other POSTs work... Looks like something's wrong
with my servlet

2009/4/9 Andrey Razumovsky <ra...@gmail.com>

> Hi friends,
>
> Problem still exists... Unforntunately I do not have a public URL. Could
> you share you HTTP POST request code.
> Lines with Content-Type, Content-Length etc are commented out because I
> tried them but they didn't help. Event if I set them, server receives GET
> with content-length=-1. Changing lines order and playing with header
> properties gave no result. And once again, my problem is NOT that Tomcat
> doesn't get POST body. It doesn't get POST at all!
> Will try multipart requests with HTTPClient, hope it'll help...
>
> Andrey
>
> 2009/4/8 André Warnier <aw...@ice-sa.com>
>
> André Warnier wrote:
>> Trying to redeem myself to Andrey for hijacking his post..
>>
>> Andrey, in your (latest) client code you do not set either a
>> content-length, nor a "chunked" encoding headers.
>> Is it possible that Tomcat 6 just ignores your POST content in that case ?
>> In RFC2616, I find this in section 4.3 :
>>  The presence of a message-body in a request is signaled by the
>>   inclusion of a Content-Length or Transfer-Encoding header field in
>>   the request's message-headers. A message-body MUST NOT be included in
>>   a request if the specification of the request method (section 5.1.1)
>>   does not allow sending an entity-body in requests. A server SHOULD
>>   read and forward a message-body on any request; if the request method
>>   does not include defined semantics for an entity-body, then the
>>   message-body SHOULD be ignored when handling the request.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>

Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
Hi friends,

Problem still exists... Unforntunately I do not have a public URL. Could you
share you HTTP POST request code.
Lines with Content-Type, Content-Length etc are commented out because I
tried them but they didn't help. Event if I set them, server receives GET
with content-length=-1. Changing lines order and playing with header
properties gave no result. And once again, my problem is NOT that Tomcat
doesn't get POST body. It doesn't get POST at all!
Will try multipart requests with HTTPClient, hope it'll help...

Andrey

2009/4/8 André Warnier <aw...@ice-sa.com>

> André Warnier wrote:
> Trying to redeem myself to Andrey for hijacking his post..
>
> Andrey, in your (latest) client code you do not set either a
> content-length, nor a "chunked" encoding headers.
> Is it possible that Tomcat 6 just ignores your POST content in that case ?
> In RFC2616, I find this in section 4.3 :
>  The presence of a message-body in a request is signaled by the
>   inclusion of a Content-Length or Transfer-Encoding header field in
>   the request's message-headers. A message-body MUST NOT be included in
>   a request if the specification of the request method (section 5.1.1)
>   does not allow sending an entity-body in requests. A server SHOULD
>   read and forward a message-body on any request; if the request method
>   does not include defined semantics for an entity-body, then the
>   message-body SHOULD be ignored when handling the request.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Cannot read httpservlet's inputstream

Posted by André Warnier <aw...@ice-sa.com>.
André Warnier wrote:
Trying to redeem myself to Andrey for hijacking his post..

Andrey, in your (latest) client code you do not set either a 
content-length, nor a "chunked" encoding headers.
Is it possible that Tomcat 6 just ignores your POST content in that case ?
In RFC2616, I find this in section 4.3 :
  The presence of a message-body in a request is signaled by the
    inclusion of a Content-Length or Transfer-Encoding header field in
    the request's message-headers. A message-body MUST NOT be included in
    a request if the specification of the request method (section 5.1.1)
    does not allow sending an entity-body in requests. A server SHOULD
    read and forward a message-body on any request; if the request method
    does not include defined semantics for an entity-body, then the
    message-body SHOULD be ignored when handling the request.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by André Warnier <aw...@ice-sa.com>.
Caldarale, Charles R wrote:
>> From: André Warnier [mailto:aw@ice-sa.com]
>> Subject: Re: Cannot read httpservlet's inputstream
>>
>> Does there exist a library somewhere which allows me to feed it this
>> InputStream and which will parse the parts for me appropriately (for
>> example allowing me to determine if a given parameter is a file, and
>> handle it easily ) ?
> 
> Why not just use this?
> 
> http://commons.apache.org/fileupload/
> 
Great !  Thanks for the link.
The irony is (now that I look back) that this was mentioned in the very 
first post of Andrey, the OP.  So he already knew that..
Big circle to get back to the starting point.
Sorry Andrey, I got carried away.
By the way, did you find a solution for your problem yet ?


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Cannot read httpservlet's inputstream

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: André Warnier [mailto:aw@ice-sa.com]
> Subject: Re: Cannot read httpservlet's inputstream
> 
> Does there exist a library somewhere which allows me to feed it this
> InputStream and which will parse the parts for me appropriately (for
> example allowing me to determine if a given parameter is a file, and
> handle it easily ) ?

Why not just use this?

http://commons.apache.org/fileupload/

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


Re: Cannot read httpservlet's inputstream

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

André,

On 4/7/2009 5:56 PM, André Warnier wrote:
> Now suppose I /do/ a POST with multipart/form-data encoding to a Tomcat
> servlet, and one of the parts /is/ a large file.
> How can I handle this at the servlet level ?

Of course you can. Haven't we been over this one already? ;)

> All I see in the Servlet Spec is HttpRequest.getParameter(), which
> returns a string (already URL-decoded), which seems hardly appropriate
> for uploading a file.

...especially because the servlet spec says that it will only decode
form parameters if the content-type is
application/x-www-form-urlencoded. If you use multipart/form-data, then
your servlet has to read the entire request body using
request.getReader() or request.getInputStream() and parse-out its own
parameters AND file content.

> Or else HttpRequest.getInputStream(), which returns a byte stream.  But
> then I guess I have to do the whole multipart/form-data parsing myself.

Exactly. There are libraries that can handle this sort of stuff for you,
but it's not part of the standard servlet API. Think of this as
HTTP::multipart which you have to download from JPAN and install. :)

> Does there exist a library somewhere which allows me to feed it this
> InputStream and which will parse the parts for me appropriately (for
> example allowing me to determine if a given parameter is a file, and
> handle it easily ) ?

There are several. Struts 2 has one built-into itself, as do other
application frameworks. Here are several standalone ones:

Super old-skool, still maintained:
http://www.servlets.com/cos/index.html

Newer, and probably more widely used:
http://commons.apache.org/fileupload/

I'm sure there are others. IMO there's no reason to look beyond
commons-fileupload.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAknbz3UACgkQ9CaO5/Lv0PCtFQCeOqQnQUBeD3mpb6ZQlxKu1SrU
qo0AoLXQGCqMHT6ejyHr9O4xTmwZgtSF
=II8q
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by André Warnier <aw...@ice-sa.com>.
Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> André,
> 
> On 4/7/2009 4:49 PM, André Warnier wrote:
>> Ok Mark, I don't pretend I know how it works in Tomcat.
>> How does one upload a file then ?
> 
> That depends on how you /want/ to upload it. If you want to use the
> standard HTML form-based file upload, you'll have to use
> multipart/form-data (since that's what the HTML spec says to do).
> 
> PUT is, as stated, also an option.
> 
> The only real difference is the meaning of the target URI.
> 
>> I mean other than with a PUT, as I presume that the OP doesn't just want
>> to write files arbitrarily in his URI space on the server)
> 
> If your server is expecting a single file for upload, then there is no
> need to go through the whole multipart/form-data thing. You can just use
> the Content-Type header to indicate that the file is
> application/x-msword or whatever and jam the file into the request body.
> 
> multipart/form-data is only useful if you want both discrete data (like
> form parameters) plus something huge, like the contents of a file. It's
> just like an email message: there's no reason to use a multipart MIME
> message when there's only one part to transmit.
> 
Ok, got it, thanks Chris (and Mark).
Now suppose I /do/ a POST with multipart/form-data encoding to a Tomcat 
servlet, and one of the parts /is/ a large file.
How can I handle this at the servlet level ?
All I see in the Servlet Spec is HttpRequest.getParameter(), which 
returns a string (already URL-decoded), which seems hardly appropriate 
for uploading a file.
Or else HttpRequest.getInputStream(), which returns a byte stream.  But 
then I guess I have to do the whole multipart/form-data parsing myself.

Does there exist a library somewhere which allows me to feed it this 
InputStream and which will parse the parts for me appropriately (for 
example allowing me to determine if a given parameter is a file, and 
handle it easily ) ?


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

André,

On 4/7/2009 4:49 PM, André Warnier wrote:
> Ok Mark, I don't pretend I know how it works in Tomcat.
> How does one upload a file then ?

That depends on how you /want/ to upload it. If you want to use the
standard HTML form-based file upload, you'll have to use
multipart/form-data (since that's what the HTML spec says to do).

PUT is, as stated, also an option.

The only real difference is the meaning of the target URI.

> I mean other than with a PUT, as I presume that the OP doesn't just want
> to write files arbitrarily in his URI space on the server)

If your server is expecting a single file for upload, then there is no
need to go through the whole multipart/form-data thing. You can just use
the Content-Type header to indicate that the file is
application/x-msword or whatever and jam the file into the request body.

multipart/form-data is only useful if you want both discrete data (like
form parameters) plus something huge, like the contents of a file. It's
just like an email message: there's no reason to use a multipart MIME
message when there's only one part to transmit.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAknbwg0ACgkQ9CaO5/Lv0PB9vACcC/UIPdCE9DwQglm78OORn+Eu
kJYAoL59rNOTErwLU2iK6aHm0YOY8XVF
=ZL/Y
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by André Warnier <aw...@ice-sa.com>.
Mark Thomas wrote:
> André Warnier wrote:
>> Despite what you may wish and what you may have found in Google, I
>> believe that if you want to send a file, you have to do it with a
>> "multipart/form-data" content type.
> 
> Nope. That is just plain wrong.
> 
>> http://www.faqs.org/rfcs/rfc2616.html
>> Sections 9.5 (POST) and 9.6 (PUT) describe the two methods that allow
>> you to upload a file.  But 9.6 is not applicable, since you are sending
>> this to a webapp, not directly to a URI.
> 
> Wrong again. PUT is disabled by default for security reasons but it is
> valid and is supported by the default servlet.
> 
Ok Mark, I don't pretend I know how it works in Tomcat.
How does one upload a file then ?

I mean other than with a PUT, as I presume that the OP doesn't just want 
to write files arbitrarily in his URI space on the server)

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Mark Thomas <ma...@apache.org>.
André Warnier wrote:
> Despite what you may wish and what you may have found in Google, I
> believe that if you want to send a file, you have to do it with a
> "multipart/form-data" content type.

Nope. That is just plain wrong.

> http://www.faqs.org/rfcs/rfc2616.html
> Sections 9.5 (POST) and 9.6 (PUT) describe the two methods that allow
> you to upload a file.  But 9.6 is not applicable, since you are sending
> this to a webapp, not directly to a URI.

Wrong again. PUT is disabled by default for security reasons but it is
valid and is supported by the default servlet.

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by André Warnier <aw...@ice-sa.com>.
Andrey Razumovsky wrote:
> Well, I've been looking through those 15 lines last two days.. I tried
> Apache's HttpClient with same effect (I used FileEntity for body, not
> multipart). I've seen many examples in Google which send post request.. My
> code is just the same..
Hi.
I did some research, because I wanted to wrap up a number of recent 
discussions on this forum.

Despite what you may wish and what you may have found in Google, I 
believe that if you want to send a file, you have to do it with a 
"multipart/form-data" content type.  That is because it is the only way 
to let the server know what kind of file your are sending, and how it is 
encoded.  The Apache HttpClient library will let you do that. I do that 
in a couple of application, and I never had a problem with Tomcat 4 and 
5.  Have not tried it yet with Tomcat 6, but this is so basic that I 
doubt there would be a problem.
Have a look here :

http://www.faqs.org/rfcs/rfc2616.html
Sections 9.5 (POST) and 9.6 (PUT) describe the two methods that allow 
you to upload a file.  But 9.6 is not applicable, since you are sending 
this to a webapp, not directly to a URI.
There are also
http://www.faqs.org/rfcs/rfc2388.html
http://www.faqs.org/rfcs/rfc2046.html
http://www.faqs.org/rfcs/rfc2046.html
Then there is http://www.w3.org/TR/html401/interact/forms.html
which is the only document that I have found which describes a bit more 
in detail how you should POST a file via HTTP.
Look at section
17.13.4 Form content types

Now, the funny part is that I don't find a corresponding 
request-handling method on the server side for such a POST..
Not in the servlet specs, that is.
?
It seems that I need some more Googling myself.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
Well, I've been looking through those 15 lines last two days.. I tried
Apache's HttpClient with same effect (I used FileEntity for body, not
multipart). I've seen many examples in Google which send post request.. My
code is just the same..
And I found a nasty thing - when I run my request against GWT's embedded
Tomcat (and I think it's 5.x) everything works just fine!! So it's likely
problem either in my Tomcat 6's settings or in Tomcat 6 itself

Will continue my research and post here, but I'd greatly appreciate if
someone tried to POST to Tomcat 6 with java client (or perhaps you have such
JUnit test?)

Andrey

2009/4/7 David Smith <dn...@cornell.edu>

> This smells like tomcat is behaving absolutely correct, but your client
> code isn't.  Can you verify your client code is properly making the
> request and handling the response?  It could be you aren't using
> HttpURLConnection correctly to upload a file.  As much as you are trying
> to avoid it, you are probably going to have to invest some quality time
> in the libraries, etc.  that properly support multipart file upload
> requests.
>
> --David
>
> Andrey Razumovsky wrote:
> > Sure.
> > That's where my investigations lead me to:
> > I'm sending POST request from Java (see code in my first message) to
> Tomcat
> > 6. But, somehow, the request comes there is GET (!) Also Content-Length,
> > Content-Type and other header parameters are reset to default values (see
> > valve trace in my second message). And mainly, servlet's input stream is
> > empty.
> > When I do POST from simple HTML,everything's fine.
> >
> > Hope you'll help me to figure out who's replacing request's header and
> > content
> >
> > Andrey
> >
> > 2009/4/7 Caldarale, Charles R <Ch...@unisys.com>
> >
> >
> >>> From: Andrey Razumovsky [mailto:razumovsky.andrey@gmail.com]
> >>> Subject: Re: Cannot read httpservlet's inputstream
> >>>
> >>> I figured out that the problem is in client side...
> >>> When I fire POST request from HTML, it is received well...
> >>> I've doublechecked my connection code - it seems all right.
> >>> Can anyone help me here?
> >>>
> >> After your various contradictory messages, I have no idea what your
> problem
> >> is at the moment.  Would you mind restating it?
> >>
> >>  - Chuck
> >>
> >>
> >> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> >> MATERIAL and is thus for use only by the intended recipient. If you
> received
> >> this in error, please contact the sender and delete the e-mail and its
> >> attachments from all computers.
> >>
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Cannot read httpservlet's inputstream

Posted by David Smith <dn...@cornell.edu>.
This smells like tomcat is behaving absolutely correct, but your client
code isn't.  Can you verify your client code is properly making the
request and handling the response?  It could be you aren't using
HttpURLConnection correctly to upload a file.  As much as you are trying
to avoid it, you are probably going to have to invest some quality time
in the libraries, etc.  that properly support multipart file upload
requests.

--David

Andrey Razumovsky wrote:
> Sure.
> That's where my investigations lead me to:
> I'm sending POST request from Java (see code in my first message) to Tomcat
> 6. But, somehow, the request comes there is GET (!) Also Content-Length,
> Content-Type and other header parameters are reset to default values (see
> valve trace in my second message). And mainly, servlet's input stream is
> empty.
> When I do POST from simple HTML,everything's fine.
>
> Hope you'll help me to figure out who's replacing request's header and
> content
>
> Andrey
>
> 2009/4/7 Caldarale, Charles R <Ch...@unisys.com>
>
>   
>>> From: Andrey Razumovsky [mailto:razumovsky.andrey@gmail.com]
>>> Subject: Re: Cannot read httpservlet's inputstream
>>>
>>> I figured out that the problem is in client side...
>>> When I fire POST request from HTML, it is received well...
>>> I've doublechecked my connection code - it seems all right.
>>> Can anyone help me here?
>>>       
>> After your various contradictory messages, I have no idea what your problem
>> is at the moment.  Would you mind restating it?
>>
>>  - Chuck
>>
>>
>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
>> MATERIAL and is thus for use only by the intended recipient. If you received
>> this in error, please contact the sender and delete the e-mail and its
>> attachments from all computers.
>>
>>
>>     

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrey,

On 4/7/2009 10:12 AM, Andrey Razumovsky wrote:
>         HttpURLConnection connect = (HttpURLConnection)
>                                     url.openConnection();
>         connect.setRequestMethod("POST");
>         connect.setDoInput(true);
>         connect.setDoOutput(true);

I have code that makes an HTTP connection, and I have the
setDoOutput(true) /before/ setRequestMethod("POST"). I'm not sure if
that is significant.

Also, you mentioned that Content-Type and Content-Length were being set
to "the defaults". I don't see any setting of these header fields in
your code. In your original code snippet, they are commented-out.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAknbnVoACgkQ9CaO5/Lv0PAZbACfZzbpItCeFngJPZZnpgVgNJeq
u9kAnixPyxxolDv2GGNhmFe1Q+6/QT3Q
=o8ko
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
I ran Tomcat on localhost and everything's the same. I don't have firewall
and I disabled antivirus, so there's nothing between client and tomcat. So
the problem is either in client code (or JDK?) or in Tomcat. I'm posting
full client code once again to show how I'm connecting to server

Thanks,
Andrey

--------------
public static void main(String[] args) throws Exception {
        String address = "http://localhost:8097/Dove";
        File pack = new File("build.xml");

        URL url = new URL(address);
        HttpURLConnection connect = (HttpURLConnection)
url.openConnection();
        connect.setRequestMethod("POST");
        connect.setDoInput(true);
        connect.setDoOutput(true);
        connect.setUseCaches(false);

        OutputStream out = connect.getOutputStream();
        FileInputStream in = new FileInputStream(pack);
        IOUtils.copy(in, out);
        out.flush();
        in.close();

        if (connect.getResponseCode() != HttpURLConnection.HTTP_OK) {
            System.err.println("file " + pack.getPath() + " was not
transfered - response code " +
                    connect.getResponseCode());
        }
        else {
            System.out.println("file " + pack.getPath() + " transfered");
        }
    }

2009/4/7 Caldarale, Charles R <Ch...@unisys.com>

> > From: Andrey Razumovsky [mailto:razumovsky.andrey@gmail.com]
> > Subject: Re: Cannot read httpservlet's inputstream
> >
> > I'm sending POST request from Java (see code in my first message)
> > to Tomcat 6. But, somehow, the request comes there is GET (!) Also
> > Content-Length, Content-Type and other header parameters are reset
> > to default values
>
> Tomcat won't be doing this.  What else is between your client program and
> Tomcat?  (E.g., proxies, httpd, smart (?) firewall?)
>
> > And mainly, servlet's input stream is empty.
> > When I do POST from simple HTML,everything's fine.
>
> Try running Wireshark or equivalent on both the client box and the Tomcat
> box to see exactly what's being put on the wire from the points of view of
> both ends.
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you received
> this in error, please contact the sender and delete the e-mail and its
> attachments from all computers.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

RE: Cannot read httpservlet's inputstream

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Andrey Razumovsky [mailto:razumovsky.andrey@gmail.com]
> Subject: Re: Cannot read httpservlet's inputstream
> 
> I'm sending POST request from Java (see code in my first message)
> to Tomcat 6. But, somehow, the request comes there is GET (!) Also
> Content-Length, Content-Type and other header parameters are reset
> to default values

Tomcat won't be doing this.  What else is between your client program and Tomcat?  (E.g., proxies, httpd, smart (?) firewall?)

> And mainly, servlet's input stream is empty.
> When I do POST from simple HTML,everything's fine.

Try running Wireshark or equivalent on both the client box and the Tomcat box to see exactly what's being put on the wire from the points of view of both ends.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
Sure.
That's where my investigations lead me to:
I'm sending POST request from Java (see code in my first message) to Tomcat
6. But, somehow, the request comes there is GET (!) Also Content-Length,
Content-Type and other header parameters are reset to default values (see
valve trace in my second message). And mainly, servlet's input stream is
empty.
When I do POST from simple HTML,everything's fine.

Hope you'll help me to figure out who's replacing request's header and
content

Andrey

2009/4/7 Caldarale, Charles R <Ch...@unisys.com>

> > From: Andrey Razumovsky [mailto:razumovsky.andrey@gmail.com]
> > Subject: Re: Cannot read httpservlet's inputstream
> >
> > I figured out that the problem is in client side...
> > When I fire POST request from HTML, it is received well...
> > I've doublechecked my connection code - it seems all right.
> > Can anyone help me here?
>
> After your various contradictory messages, I have no idea what your problem
> is at the moment.  Would you mind restating it?
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you received
> this in error, please contact the sender and delete the e-mail and its
> attachments from all computers.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

RE: Cannot read httpservlet's inputstream

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Andrey Razumovsky [mailto:razumovsky.andrey@gmail.com]
> Subject: Re: Cannot read httpservlet's inputstream
> 
> I figured out that the problem is in client side...
> When I fire POST request from HTML, it is received well...
> I've doublechecked my connection code - it seems all right.
> Can anyone help me here?

After your various contradictory messages, I have no idea what your problem is at the moment.  Would you mind restating it?

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
I figured out that the problem is in client side... When I fire POST request
from HTML, it is received well... I've doublechecked my connection code - it
seems all right. Can anyone help me here?

Re: Cannot read httpservlet's inputstream

Posted by Andrey Razumovsky <ra...@gmail.com>.
Hi Chris,

I've found that my servlet is receiving request GET instead-of POST. Maybe
this is causing the problem.. I used to get 200, but now when I removed
doGet(...) I get 405. Maybe I need to tune Tomcat somehow so that it could
receive POST?

07.04.2009 12:36:37 RequestDumperValve invoke : REQUEST URI       =/Dove/
07.04.2009 12:36:37 RequestDumperValve invoke :           authType=null
07.04.2009 12:36:37 RequestDumperValve invoke :  characterEncoding=null
07.04.2009 12:36:37 RequestDumperValve invoke :      contentLength=-1
07.04.2009 12:36:37 RequestDumperValve invoke :        contentType=null
07.04.2009 12:36:37 RequestDumperValve invoke :        contextPath=/Dove
07.04.2009 12:36:37 RequestDumperValve invoke :
header=cache-control=no-cache
07.04.2009 12:36:37 RequestDumperValve invoke :
header=pragma=no-cache
07.04.2009 12:36:37 RequestDumperValve invoke :
header=user-agent=Java/1.6.0_03
07.04.2009 12:36:37 RequestDumperValve invoke :             header=host=
192.168.1.82:8097
07.04.2009 12:36:37 RequestDumperValve invoke :
header=accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
07.04.2009 12:36:37 RequestDumperValve invoke :
header=connection=keep-alive
07.04.2009 12:36:37 RequestDumperValve invoke :             locale=ru_RU
07.04.2009 12:36:37 RequestDumperValve invoke :             method=GET
07.04.2009 12:36:37 RequestDumperValve invoke :
parameter=filename=lastchanges_66806_1239091498437_14.zip
07.04.2009 12:36:37 RequestDumperValve invoke :
parameter=operation=upload
07.04.2009 12:36:37 RequestDumperValve invoke :
parameter=pwd=72b0750fe332f6d0012038f847ff6ecc
07.04.2009 12:36:37 RequestDumperValve invoke :           pathInfo=null
07.04.2009 12:36:37 RequestDumperValve invoke :           protocol=HTTP/1.1
07.04.2009 12:36:37 RequestDumperValve invoke :
queryString=operation=upload&filename=lastchanges_66806_1239091498437_14.zip&pwd=72b0750fe332f6d0012038f847ff6ecc
07.04.2009 12:36:37 RequestDumperValve invoke :
remoteAddr=192.168.1.82
07.04.2009 12:36:37 RequestDumperValve invoke :
remoteHost=192.168.1.82
07.04.2009 12:36:37 RequestDumperValve invoke :         remoteUser=null
07.04.2009 12:36:37 RequestDumperValve invoke : requestedSessionId=null
07.04.2009 12:36:37 RequestDumperValve invoke :             scheme=http
07.04.2009 12:36:37 RequestDumperValve invoke :
serverName=192.168.1.82
07.04.2009 12:36:37 RequestDumperValve invoke :         serverPort=8097
07.04.2009 12:36:37 RequestDumperValve invoke :        servletPath=/
07.04.2009 12:36:37 RequestDumperValve invoke :           isSecure=false
07.04.2009 12:36:37 RequestDumperValve invoke :
---------------------------------------------------------------
07.04.2009 12:36:37 RequestDumperValve invoke :
---------------------------------------------------------------
07.04.2009 12:36:37 RequestDumperValve invoke :           authType=null
07.04.2009 12:36:37 RequestDumperValve invoke :      contentLength=-1
07.04.2009 12:36:37 RequestDumperValve invoke :
contentType=text/html;charset=utf-8
07.04.2009 12:36:37 RequestDumperValve invoke :
cookie=JSESSIONID=AEAFADEEAA7D853CD106CB922F69AA71; domain=null; path=/Dove
07.04.2009 12:36:37 RequestDumperValve invoke :
header=Set-Cookie=JSESSIONID=AEAFADEEAA7D853CD106CB922F69AA71; Path=/Dove
07.04.2009 12:36:37 RequestDumperValve invoke :            message=HTTP
method GET is not supported by this URL
07.04.2009 12:36:37 RequestDumperValve invoke :         remoteUser=null
07.04.2009 12:36:37 RequestDumperValve invoke :             status=405
07.04.2009 12:36:37 RequestDumperValve invoke :
===============================================================

---------------------------------------------------------
Here's my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <display-name>Dove</display-name>

    <servlet>
        <servlet-name>DoveServlet</servlet-name>
        <servlet-class>com.nic.dove.web.DoveServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DoveServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
---------------------------------------------

And here's my server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="-1" shutdown="SHUTDOWN">
    <Listener SSLEngine="on"
className="org.apache.catalina.core.AprLifecycleListener"/>
    <Listener className="org.apache.catalina.core.JasperListener"/>
    <Listener
className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
    <Listener
className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Service name="Catalina">
        <Connector URIEncoding="UTF-8" connectionTimeout="20000"
            port="8097" protocol="HTTP/1.1"/>
        <Engine defaultHost="localhost" name="Catalina">
            <Host appBase="webapps" autoDeploy="true" name="localhost"
                unpackWARs="true" xmlNamespaceAware="false"
xmlValidation="false"/>
            <Valve
className="org.apache.catalina.valves.RequestDumperValve"/>
        </Engine>
    </Service>
</Server>

Andrey