You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Rooboo <rb...@heiler.com> on 2007/12/10 14:05:25 UTC

Send InputStream via PostMethod

Hello there,

I searched the internet for a solution to transfer an InputStream via the
PostMethod. I found an article in a mailinglist which is simliar to my code.
Here is the link to the article:

http://mail-archives.apache.org/mod_mbox/jakarta-httpcomponents-dev/200307.mbox/%3c723E8170-B894-11D7-8C98-000393C78E78@bluesunrise.com%3e
http://mail-archives.apache.org/mod_mbox/jakarta-httpcomponents-dev/200307.mbox/%3c723E8170-B894-11D7-8C98-000393C78E78@bluesunrise.com%3e 

Also my problem was, that the servlet couldn't get tha params i added. But
also with the new content type, it is not possible to reach the body of the
request in which I assumed the InputStream. 

Here is my code since now:
final PostMethod filePost = new PostMethod( url.toURI()
                                                       .toString() );

filePost.getParams()
        .setBooleanParameter( HttpMethodParams.USE_EXPECT_CONTINUE, true );

try
{
  final FilePart part = new FilePart( fileName, new InputStreamPartSource(
fileInputStream, fileName ) );
  part.setContentType( "multipart/form-data" ); //$NON-NLS-1$
  filePost.setRequestEntity( new MultipartRequestEntity( new Part[] { part
}, filePost.getParams() ) );
  filePost.addParameter( "url", targetDir ); //$NON-NLS-1$
  filePost.addParameter( "fileName", fileName ); //$NON-NLS-1$
  final HttpClient client = new HttpClient();
  client.getHttpConnectionManager()
        .getParams()
        .setConnectionTimeout( 5000 );
  client.getParams()
        .setAuthenticationPreemptive( true );
  final int status = client.executeMethod( filePost );

  if ( status == HttpStatus.SC_OK )
  {
  ...

And this is the servlet
final String url = req.getParameter( "url" );
final String filename = req.getParameter( "fileName" );
final InputStream iStream = req.getInputStream();
final OutputStream oStream = new FileOutputStream( new File( url, filename )
);

final byte[] buffer = new byte[10240];
int len = 0;
while ( true )
{
  len = iStream.read( buffer );
  if ( len == -1 )
  {
    break;
  }
  oStream.write( buffer, 0, len );
}
iStream.close();
oStream.close();

The code does nothing else than write the file, which should the servlet get
as a stream, to the directory which is defined in the url parameter. The
problem is, that final InputStream iStream = req.getInputStream(); returns
an empty Inputstream. Also createInputStream() in the InputStreamPartSource
is never called. Why?

Regards,
Robert
-- 
View this message in context: http://www.nabble.com/Send-InputStream-via-PostMethod-tp14252100p14252100.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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


Re: Send InputStream via PostMethod

Posted by Rooboo <rb...@heiler.com>.
Hm, the order of adding parameter and setting the request entity is very
important and also confusing.
When I add parameter first, and then set the request entity, the parameter
disappeared or better, the params field is empty. Otherwise, when I set the
request entity first, then add 
the parameters, the request entity is null again. Very strange :-S
Why should the request body always be cleared when adding parameters or
setting the request entity?
For now I override the clearRequestBody method, but this also doesn't help.
The InputStream makes it to the request body and I get it in the servlet,
but not the parameters.

Any help?


Rooboo wrote:
> 
> I searched the internet for a solution to transfer an InputStream via the
> PostMethod. I found an article in a mailinglist which is simliar to my
> code.
> 

-- 
View this message in context: http://www.nabble.com/Send-InputStream-via-PostMethod-tp14252100p14254164.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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


Re: Send InputStream via PostMethod

Posted by Rooboo <rb...@heiler.com>.
Or should i rather us the InputStreamRequestEntity instead of
MultiPartRequestEntity? Sorry I just found it in a previous post..


Rooboo wrote:
> 
> Hello there,
> 
> I searched the internet for a solution to transfer an InputStream via the
> PostMethod. I found an article in a mailinglist which is simliar to my
> code.
> Here is the link to the article:
> 
> 
> http://mail-archives.apache.org/mod_mbox/jakarta-httpcomponents-dev/200307.mbox/%3c723E8170-B894-11D7-8C98-000393C78E78@bluesunrise.com%3e
> http://mail-archives.apache.org/mod_mbox/jakarta-httpcomponents-dev/200307.mbox/%3c723E8170-B894-11D7-8C98-000393C78E78@bluesunrise.com%3e 
> 
> Also my problem was, that the servlet couldn't get tha params i added. But
> also with the new content type, it is not possible to reach the body of
> the request in which I assumed the InputStream. 
> 
> Here is my code since now:
> final PostMethod filePost = new PostMethod( url.toURI()
>                                                        .toString() );
> 
> filePost.getParams()
>         .setBooleanParameter( HttpMethodParams.USE_EXPECT_CONTINUE, true
> );
> 
> try
> {
>   final FilePart part = new FilePart( fileName, new InputStreamPartSource(
> fileInputStream, fileName ) );
>   part.setContentType( "multipart/form-data" ); //$NON-NLS-1$
>   filePost.setRequestEntity( new MultipartRequestEntity( new Part[] { part
> }, filePost.getParams() ) );
>   filePost.addParameter( "url", targetDir ); //$NON-NLS-1$
>   filePost.addParameter( "fileName", fileName ); //$NON-NLS-1$
>   final HttpClient client = new HttpClient();
>   client.getHttpConnectionManager()
>         .getParams()
>         .setConnectionTimeout( 5000 );
>   client.getParams()
>         .setAuthenticationPreemptive( true );
>   final int status = client.executeMethod( filePost );
> 
>   if ( status == HttpStatus.SC_OK )
>   {
>   ...
> 
> And this is the servlet
> final String url = req.getParameter( "url" );
> final String filename = req.getParameter( "fileName" );
> final InputStream iStream = req.getInputStream();
> final OutputStream oStream = new FileOutputStream( new File( url, filename
> ) );
> 
> final byte[] buffer = new byte[10240];
> int len = 0;
> while ( true )
> {
>   len = iStream.read( buffer );
>   if ( len == -1 )
>   {
>     break;
>   }
>   oStream.write( buffer, 0, len );
> }
> iStream.close();
> oStream.close();
> 
> The code does nothing else than write the file, which should the servlet
> get as a stream, to the directory which is defined in the url parameter.
> The problem is, that final InputStream iStream = req.getInputStream();
> returns an empty Inputstream. Also createInputStream() in the
> InputStreamPartSource is never called. Why?
> 
> Regards,
> Robert
> 

-- 
View this message in context: http://www.nabble.com/Send-InputStream-via-PostMethod-tp14252100p14252723.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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


Re: Send InputStream via PostMethod

Posted by Rooboo <rb...@heiler.com>.

Roland Weber wrote:
> 
> What MIME type are you using? I suggest something like
> application/binary or application/x-your-app-name.
> If you call request.getParameter() on the server side,
> the Servlet container might read the stream if the
> MIME type indicates parameters.
> 

I use "application/binary" because i cannot decide which type of file it is,
that i want to upload. I'm just getting an FIleInputStream. However, I'm
getting crazy with this.
What do you mean with "MIME type indicates parameters"?
-- 
View this message in context: http://www.nabble.com/Send-InputStream-via-PostMethod-tp14252100p14270437.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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


Re: Send InputStream via PostMethod

Posted by Roland Weber <os...@dubioso.net>.
Rooboo wrote:
> Ok for Now I just set the InputStreamRequestEntity, but I also don't get it
> really in the servlet, because it seems to be empty.

What MIME type are you using? I suggest something like
application/binary or application/x-your-app-name.
If you call request.getParameter() on the server side,
the Servlet container might read the stream if the
MIME type indicates parameters.

hope that helps,
  Roland


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


Re: Send InputStream via PostMethod

Posted by Rooboo <rb...@heiler.com>.
Ok for Now I just set the InputStreamRequestEntity, but I also don't get it
really in the servlet, because it seems to be empty.


Roland Weber wrote:
> 
>> Hm, the order of adding parameter and setting the request entity is very
>> important and also confusing.
> 
> No, it's not. You do one or the other, never both.
> 
>> Why should the request body always be cleared when adding parameters or
>> setting the request entity?
> 
> Because POST parameters need to be encoded in a
> request entity. Either you leave that to HttpClient,
> or you do it yourself.
> 
> See also our Application Design FAQ:
> http://wiki.apache.org/jakarta-httpclient/FrequentlyAskedApplicationDesignQuestions#head-481bfba2695be563137380a5aa6a26c7ae4d189a
> 
> cheers,
>   Roland
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Send-InputStream-via-PostMethod-tp14252100p14255674.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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