Userful, but some important missing details,
January 16, 2009 at 7:06 AM
Thanks. The tutorial was very useful and I had a basic file upload up and running in no time.
One very important piece of missing information however: you need to include the commons-filupload-1.2.jar and commons-io.1.3.1.jar files otherwise it won't work. This will fix the issue that people are having with "Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest" errors.
Other useful items. In order to change where the files get downloaded to, add a line to your struts.properties file. For example:
struts.multipart.saveDir=/tmp/uploader
Finally, the file is deleted once the operation finished. In order to store it add some code to the "execute" method. For example:
public String execute() throws Exception {
File outFile = new File("/tmp/newfilename"); InputStream inStrm = new FileInputStream(getUpload()); OutputStream outStrm = new FileOutputStream(outFile);
// Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = inStrm.read(buf)) > 0) { outStrm.write(buf, 0, len); } inStrm.close(); outStrm.close();