java - Uploading images using Servlet 3.0 -
i have simple form enctype "multipart"; using upload image server. have 2 possible solutions, neither of them complete.
first solution:
fileitemiterator iterator = upload.getitemiterator( request ); while( iterator.hasnext() ){ fileitemstream item = iterator.next(); if( item.isformfield() ){ // store img } } // ~while( iter.hasnext() )
in solution, can't dimensions of uploaded file, can if it's form field or not (using item.isformfield()
)
my second solution uses servlet 3.0 api:
for( part part: request.getparts() ){ system.out.println( part.getsize() ); }
here can size of uploaded image, can't tell whether it's simple form field or not.
what missing?
what need me?
as mentioned in documentation of fileitemstream
the isformfield() method tells whether if simple form field returns true , otherwise return false .
so upload image return false
so should following :
if(! item.isformfield() ){ // store img }else { //simple form field. }
and refer answer here see how convert inputstream byte[] know how many bytes there in uploaded file .
if using servlet 3 following :
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string username= request.getparameter("username"); // retrieves <input type="text" name="username"> part filepart = request.getpart("file"); // retrieves <input type="file" name="file"> string filename = filepart.getname(); inputstream filecontent = filepart.getinputstream(); // ... (do job here) }
and give me feedback .
hope helps .
Comments
Post a Comment