Why doesn't my file get uploaded to my java servlet? -
well i'm trying upload file servlet web interface. i've been using apache commons fileupload following tutorial , integrating servlet. somehow can't manage file uploaded.
here's form in web page:
<form class="well" action="gentreeuploader" method="post" enctype="multipart/form-data"> <label>choose file:</label> <center><input type="file" class="input-xlarge" name="wordfile"></center><br> <center><span class="help-block">note: after clicking "upload file!" of data contained in file uploaded database</span></center><br> <center><input class="btn btn-primary" type="submit" value="upload file!"></center> <input type="text" name="tester" value="xoxoxo" /> <input type="hidden" name="action" value="startupload" /> </form> then here's code part of servlet should handle file upload request:
public void artiupload(httpservletrequest request, httpservletresponse response, htmlwriter writer) throws servletexception, ioexception { system.out.println("i'm in upload"); printwriter out = response.getwriter(); // check have file upload request boolean ismultipart = servletfileupload.ismultipartcontent(request); // create factory disk-based file items // create factory disk-based file items diskfileitemfactory factory = new diskfileitemfactory(); // set factory constraints factory.setsizethreshold(maxmemsize); factory.setrepository(new file(htmlpath.tostring())); system.out.println("file path: " + htmlpath.tostring()); // create new file upload handler servletfileupload upload = new servletfileupload(factory); // set overall request size constraint upload.setsizemax(maxfilesize); system.out.println("max file size: " + maxfilesize); system.out.println("max file size in memory: " + maxmemsize); list items = null; try { // parse request items = upload.parserequest(request); system.out.println("items found: " + items.size()); out.write(writer.printcenter("file uploading done")); } catch (fileuploadexception ex) { logger.getlogger(gentreeuploader.class.getname()).log(level.severe, null, ex); out.write(writer.printcenter("file uploading failed")); } } and here tomcat server log servlet method:
i'm in upload file path: d:\dropbox\studiesnew\netbeansprojects\gentreeuploader\build\web\web-inf\classes\html max file size: 10485760 max file size in memory: 2097152 items found: 0 well i'm getting parameters request using: action = request.getparameter(actiontext);
works fine normal parameters, file doesn't uploaded. believe after parsing list should have 1 item in case, shows 0...
can help? thanks.
the full log this?
using catalina_base: "c:\users\arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base" using catalina_home: "c:\program files\apache software foundation\apache tomcat 7.0.22" using catalina_tmpdir: "c:\users\arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\temp" using jre_home: "c:\program files\java\jdk1.7.0_03" using classpath: "c:\program files\apache software foundation\apache tomcat 7.0.22\bin\bootstrap.jar;c:\program files\apache software foundation\apache tomcat 7.0.22\bin\tomcat-juli.jar" bir 05, 2012 2:45:06 org.apache.catalina.core.aprlifecyclelistener init info: apr based apache tomcat native library allows optimal performance in production environments not found on java.library.path: c:\program files\java\jdk1.7.0_03\bin;c:\windows\sun\java\bin;c:\windows\system32;c:\windows;c:\program files\common files\microsoft shared\windows live;c:\program files (x86)\common files\microsoft shared\windows live;c:\program files (x86)\nvidia corporation\physx\common;c:\windows\system32;c:\windows;c:\windows\system32\wbem;c:\windows\system32\windowspowershell\v1.0\;c:\program files (x86)\windows live\shared;c:\program files\microsoft sql server\110\tools\binn\;c:\program files (x86)\microsoft sql server\110\tools\binn\;c:\program files\microsoft sql server\110\dts\binn\;c:\program files (x86)\microsoft sql server\110\tools\binn\managementstudio\;c:\program files (x86)\microsoft sql server\110\dts\binn\;c:\program files\matlab\r2010b\runtime\win64;c:\program files\matlab\r2010b\bin;. bir 05, 2012 2:45:07 org.apache.coyote.abstractprotocol init info: initializing protocolhandler ["http-bio-8084"] bir 05, 2012 2:45:07 org.apache.coyote.abstractprotocol init info: initializing protocolhandler ["ajp-bio-8009"] bir 05, 2012 2:45:07 org.apache.catalina.startup.catalina load info: initialization processed in 865 ms bir 05, 2012 2:45:07 org.apache.catalina.core.standardservice startinternal info: starting service catalina bir 05, 2012 2:45:07 org.apache.catalina.core.standardengine startinternal info: starting servlet engine: apache tomcat/7.0.22 bir 05, 2012 2:45:07 org.apache.catalina.startup.hostconfig deploydescriptor info: deploying configuration descriptor gentreeuploader.xml c:\users\arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\catalina\localhost bir 05, 2012 2:45:08 org.apache.catalina.startup.hostconfig deploydescriptor info: deploying configuration descriptor manager.xml c:\users\arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\catalina\localhost bir 05, 2012 2:45:08 org.apache.catalina.startup.hostconfig deploydescriptor info: deploying configuration descriptor mrweb.xml c:\users\arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\catalina\localhost bir 05, 2012 2:45:08 org.apache.catalina.startup.hostconfig deploydescriptor info: deploying configuration descriptor root.xml c:\users\arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\catalina\localhost bir 05, 2012 2:45:08 org.apache.coyote.abstractprotocol start info: starting protocolhandler ["http-bio-8084"] bir 05, 2012 2:45:08 org.apache.coyote.abstractprotocol start info: starting protocolhandler ["ajp-bio-8009"] bir 05, 2012 2:45:08 org.apache.catalina.startup.catalina start info: server startup in 1396 ms i'm in upload file path: d:\dropbox\studiesnew\netbeansprojects\gentreeuploader\build\web\web-inf\classes\html max file size: 10485760 max file size in memory: 2097152 items found: 0
it fail way when request body has been parsed beforehand calling getparameter(), getreader() or getinputstream() methods on request inside dopost() method of same servlet or inside dofilter() method of 1 of servlet filters runs on same request. possible cause you're nesting html <form> elements, behaviour browser dependent.
by way, html <center> element deprecated since 1998. rid of , use css instead.
update: update confirms you're calling getparameter() on request. should not when it's multipart/form-data request. should instead use same apache commons fileupload api collect normal form fields. see how upload files server using jsp/servlet?
Comments
Post a Comment