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/[EMAIL > PROTECTED] > http://mail-archives.apache.org/mod_mbox/jakarta-httpcomponents-dev/200307.mbox/[EMAIL > PROTECTED] > > 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]