hi i'm cameron mckenzie at cameron mcnz on twitter and i want to show you how to upload files
with struts so i want to show you how the file upload works in struts and i'm starting off here
with a simple struts project it was created with maven so it's just a basic maven project with the
required dependencies there struts core struts convention and the servlet api a basic plug-in
configuration there to force 1.8 jdk compliance and you can see i've got a web.xml file here which
is just the standard struts mapping for struts 2.5 making sure that i've got the struts prepare and
execute filter configured there and so that's where you want to start off now you are going
to need a couple of dependencies in order to get the struts file upload facilities to work you need
to add in commons file upload and commons io now everything's done through struts it's just struts
needs these particular packages in order for the struts upload facilities to work now how does a
struts get you to upload a file make it possible to do that through the browser well the key is the
struts form for uploading a file so i'm going to create a new jsp page here i'm going to call it
index.jsp right there in the web app folder and i'm going to add my taglib and page directive
here get rid of the basic stuff that it gives me and start off from scratch so i'm going to
include the struts tag and basic page directive i'll throw in the requisite html and body tags
and then in here is really where the meat of the movement happens what you need is you need
this form tag and i'm going to copy and paste this in here and i guess we've got to end that
form tag to make everybody happy the method is post and the encryption type is multi-part
that's standard that should stay the same for any single file upload that you're going to do and
then the action well that's going to be the name of the action class that i create on the server
to support this i'm going to create that action class in just a minute but that has to point to an
action component on your server and then there's a couple of entries that you need in here
you need to specify the name of the file being uploaded uploaded file becomes very important
that it becomes extremely important later because i need to have a property of type
file in the action i'm going to create that maps to this i also have to have a property called
whatever this is and then file name and since i entered this in file it's actually going to
be a setter name set uploaded file file name which is going to sound a bit redundant a bit redundant
but it will still work so you'll see how it goes now this is it that is the key you can decorate
this with whatever html you want but this is the key to getting a form upload configured
inside of struts it's that form tag now i do need an action named file upload and that action has
to have a property of type file named uploaded file and actually doesn't need a property with
that name it needs a setter with that name to be explicit doesn't need a property just needs the
setter you can name the property whatever you want i'm going to name it the same thing just
because i think it's going to look nice but you'll see the names will differ when i go in and create
the the file name property okay so let's do that let's create that so i'm going to create a new
class it is going to be in the com.mcnz dot struts package make sure your package ends with struts
otherwise the annotations won't find it i'm going to call this class my file upload action and i'm
going to make it a sub class of action support so browse fraction support and there it is there
and i think that's everything that i need okay that looks good to begin with now
this will be mapped to the action file upload remember my jsp i said the action for this form
is file upload that means i have to specify that the action here is file upload as well
i'll organize my imports in just a moment it can't find that so i need to do an import
and also if the execute method that i'm going to have in here successful i'm going to forward
the processing to a page called results.jsp so there's a couple of things you need to do just
to set up the entire action class now as i said it's barking at me because of a couple of missed
imports so i'll click organize imports import that action class from struts2 and then those
errors will go away i think that just wants me to put in a serialization id and i will reject that
but it's just a warning warning so it's not a big deal now as i mentioned over here
the name of the file is uploaded file that means i need to declare a method
called set uploaded file and it just makes sense that i should also have a property that will catch
that so i'm going to declare a property called uploaded file of type file and then i will add
a setter for that and it's the setter that the stretch framework is going to call the setter
will not initialize this file by its name it'll find the matching setter that corresponds to this
property here and pass the file object into it and then uploaded file will get assigned to that
now i do believe that's barking because i don't have java.io imported so i will organize my
imports import java.io.file and those errors will go away now the other thing that it the uploaded
file doesn't give you the name of the file that is being uploaded so you want to actually have a
property that maps to the name of the uploaded file i'm going to call it uploaded file name
now the method is actually the name of the file as specified here plus the word
file name which means i'm going to have to have a setter called uploaded file
set uploaded file file name so the word file is going to end up in there twice
but in my property i'm not doing that because that sounds redundant it sounds redundant so
you'll see what i mean here i'm going to have to have a method called set uploaded file file name
so it's called set uploaded file file name and again
just because you have to append file name to whatever is listed here as the name of the file
but i set uploaded file name with only one file in it to the name of the file being passed in
here but this now gives me the name of the file as it's uploaded and it actually gives me the file
now from here well you can code and execute method and you can do whatever you want
in your execute method now it just makes sense to me that in your execute method you would probably
just want to save that file so i'm going to create a new variable up here called local directory
and i'm going to set it to c colon slash temp so that's where i'm going to upload all of my files
and in the execute method what i'm going to do is i'm going to create a new local file
and that's a local file output stream based on that local directory so ctemp and the name of the
uploaded file so if someone uploads a file called image.jpg i will now create an output stream to
c temp slash image.jpg and then i will save the file that was passed in that uploaded file into
that directory there so that's what happens here i create a new file output stream essentially
to save the uploaded file and then i call the file utils object and its copy file method to take that
uploaded file and save it locally i'm going to do an organize imports to get rid of those errors
source organize imports click control s and now this allows me to basically take that file that
was uploaded and save it into the temp directory and so that's the meat of the method there
and now what do i have to do in my final page well i really don't have to do much
i need a results.jsp page i'm going to create a a new jsp called results.jsp
and i'm not going to do much i'm going to paste some code in you can find this code
in github it's also on the server side and what i'm just going to do is i'm going to say
to the user hey the file was uploaded properly so uploaded file name that's a property here
of the action class so i can access any of these properties in the jsp that i forward to
notice that that results.jsp is forwarded to i don't know forwards a good name it's rendered
upon success so we've accessed all of those properties and so this will just give the user
that information that hey this was uploaded that file was uploaded properly and there we
go that's the whole thing so now all i have to do is just take this file oh i'm going to do a maven
update project just to make sure that
all of the libraries in the palm file have been updated so that looks good there and
then i will do a run as maven install that just makes me feel better although i'm not sure if
that's actually necessary i think maybe when i run this it'll do a maven install anyways but
old habits die hard and now i'll say run on server for this jsp and deploy it to the tomcat server
and you can see this file directory comes up now i'm actually going to take that file name and i'm
not going to upload it right there i'm actually going to paste this url into chrome okay so now
we'll see how it works in chrome and i'm also going to bring up that temp directory so you can
actually see right there there's my temp directory with no files in it so i'm going to choose a file
so i will choose a file and then why don't i see there's my friend cali there
and there's a copy of my book so i'll take that and i will upload it click submit
and now it says the file was uploaded successfully and then you can see the
file right there although actually the property isn't coming through and let's check why that is
oh and actually it's pretty obvious the property here in the results jsp
is uploaded file name i've got a property called uploaded file name but i don't have a getter for
it you need the getter for it in order for struts to be able to find it so i need to just go source
generate getters and setters and make sure that i have a getter for that uploaded file name
click control s just do a quick restart of the server
and then run that one more time bring up localhost
bring up the temp directory do a refresh we'll actually do a back button do a refresh
and now let's see if i can oh upload a that picture of cali click submit
and now it says the cali selfie was uploaded successfully and you can see it right there
now i will say in the github directory or even on the server side i did add an extra method and
so i added this cool method in here that does a directory listing and returns a file list
so if i take a look at the file upload action i added this in this will actually go into the
local directory and return a set showing all of the different files that are in that directory so
i'm actually going to go to that directory and just delete those two files for now
i'm going to do an organizing of the imports once again lots of neat stuff has been added there so
organize imports that's the java.util set that i want click control s but now i've got this method
called get file list and what i was thinking was instead of just doing hey you know you uploaded
that successfully why not given the chance to upload more files and even do a directory listing
so i get kind of crazy on the server side and i get kind of crazy if you look at the code in
the github account i'm just going to paste it on in here just to show you what it looks like
but instead of just this little message here what i've done is i've i've put the file upload back on
so that code there is just this code here it's all formatted on a couple of lines but that's the file
upload again and it uploaded file yeah it's the right name and then what i do was i say hey the
file was uploaded successfully so long as that uploaded file name is there i guess it should
always be there so maybe that if statement isn't necessary and then i have a little heading for a
directory listing and what i do in the directory listing is if there's nothing in the directory i
say hey there's no files in the director there should be files in the directory just like
that uploaded file name should come up but i guess potentially maybe somebody would get to this
jsp directly and they wouldn't be and then if there are files in there what
we will do is we will go over this file list and again that file list comes from
this get file list method here inside of the file upload action class and then we list out
all of the files in that directory so i'm going to save that i'll restart the server
just to make sure that we're getting the
latest i'm going to click the back button and refresh on that page i'll take a look at this test
temp folder and now i'm going to choose a file and i'm going to upload a picture of san francisco
and now it says hey there's one file in the directory and i guess that's what happens when
democrats run your city and then i'm going to choose another file take a look at cali there
and i will submit that now the cali selfie is in there and then i don't know why don't we
choose that pickering a springfield cover right there and submit it as well and so now i think
i've got a much nicer application it's not only uploading files but i don't have to click the back
button to upload them again i can do one after another and then it even gives me a listing of
all the files that are inside of this directory and that i think that's a pretty good coverage
of the struts file upload use case and there you go that's how easy it is to upload files
using struts now if you enjoyed that tutorial i want you to head over to theserverside.com i'm
the editor-in-chief over there got lots of great tutorials on struts hibernate
spring all enterprise frameworks anything that has to do with java in general if you're
interested in my personal antics you can follow me on twitter at cameronmcnz and subscribe on youtube