hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rooboo <rbarth...@heiler.com>
Subject Send InputStream via PostMethod
Date Mon, 10 Dec 2007 13:05:25 GMT

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


Mime
View raw message