1. Upload a single file
Uploading files is a feature of many web programs. In struts1.x, you have provided a component for uploading files. Struts2 provides a file upload component that is easier to operate. The difference is that the struts1.x Upload Component requires an actionform to pass files, while the struts2 Upload Component is an interceptor (this interceptor is automatically loaded without configuration ). This article describes how to use struts2 to upload a single file, and finally how to use struts2 to upload any number of files.
Using struts2 to upload a single file is very easy to implement, as long as you use a common action. However, to obtain information about the uploaded files, such as the file name, file type, and stream object, you need to add some getter and setter methods for the action class according to certain rules.
In struts2, it is used to obtain and set Java. io. file object (struts2 uploads the file to a temporary path and uses Java. io. file to open this temporary file) the methods are getupload and setupload. Getuploadfilename and setuploadfilename are used to obtain and set the object content. getuploadcontenttype and setuploadcontenttype are used to obtain and set the object content. The complete code for the upload class is as follows:
Packageaction;
Importjava. Io .*;
Importcom. opensymphony. xwork2.actionsupport;
Publicclassuploadactionextendsactionsupport
{
Privatefileupload;
Privatestringfilename;
Privatestringuploadcontenttype;
Publicstringgetuploadfilename ()
{
Returnfilename;
}
Publicvoidsetuploadfilename (stringfilename)
{
This. filename = filename;
}
Publicfilegetupload ()
{
Returnupload;
}
Publicvoidsetupload (fileupload)
{
This. Upload = upload;
}
Publicvoidsetuploadcontenttype (stringcontenttype)
{
This. uploadcontenttype = contenttype;
}
Publicstringgetuploadcontenttype ()
{
Returnthis. uploadcontenttype;
}
Publicstringexecute () throwsexception
{
Java. Io. inputstreamis = newjava. Io. fileinputstream (upload );
Java. Io. outputstreamos = newjava. Io. fileoutputstream ("D: Upload" + filename );
Bytebuffer [] = newbyte [8192];
Intcount = 0;
While (COUNT = is. Read (buffer)> 0)
{
OS. Write (buffer, 0, count );
}
OS. Close ();
Is. Close ();
Returnsuccess;
}
}
The implementation code in the execute method is very simple, just copying from the temporary file to the specified path (here is D: Upload. The default value of the temporary directory for uploading files is javax. servlet. context. tempdir value, but you can use struts. properties (and struts. struts. multipart. set the savedir attribute. The default size limit of struts2 files to be uploaded is 2 MB (2097152 bytes). You can also use struts. struts. multipart. modify maxsize, such as struts. multipart. maxsize = 2048 indicates that the total size of an uploaded file cannot exceed 2 K bytes.
The following code is the JSP page code for uploading files:
<% @ Pagelanguage = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<% @ Taglibprefix = "S" uri = "/Struts-tags" %>
<HTML>
<Head>
<Title> upload a single file </title>
</Head>
<Body>
<S: formaction = "Upload" namespace = "/test"
Enctype = "multipart/form-Data">
<S: filename = "Upload" label = "Enter the file name to be uploaded"/>
<S: submitvalue = "Upload"/>
</S: Form>
</Body>
</Html>
You can also use <s: Property> On the success. jsp page to obtain the file attributes (File Name and file content type). The Code is as follows:
<S: property value = "uploadfilename"/>
Ii. upload any number of files
In struts2, it is also very easy to upload any number of files. First, to upload any number of files, you must use DOM technology on the client to generate any number of <input type = "file"/> tags. The name attribute values are the same. The Code is as follows:
<HTML>
<Head>
<Scriptlanguage = "JavaScript">
Functionaddcomponent ()
{
Varuploadhtml = Document. createelement ("<inputtype = 'file' name = 'upload'/> ");
Document. getelementbyid ("Files"). appendchild (uploadhtml );
Uploadhtml = Document. createelement ("<p/> ");
Document. getelementbyid ("Files"). appendchild (uploadhtml );
}
</SCRIPT>
</Head>
<Body>
<Inputtype = "button" onclick = "addcomponent ();" value = "add file"/>
<Br/>
<Formonsubmit = "returntrue;" Action = "/struts2/test/upload. Action"
Method = "Post" enctype = "multipart/form-Data">
<Spanid = "Files"> <inputtype = 'file' name = 'upload'/>
<P/>
</Span>
<Inputtype = "Submit" value = "Upload"/>
</Form>
</Body>
</Html>
The above JavaScript code can generate any number of <input type = 'file'> labels, and the value of name is file (note that the above JavaScript code is only applicable to IE browsers, other browsers such as Firefox need to use their code ). As for the action class and the action class for uploading a single file, you only need to change the type of the three attributes to list. The Code is as follows:
Packageaction;
Importjava. Io .*;
Importcom. opensymphony. xwork2.actionsupport;
Publicclassuploadmoreactionextendsactionsupport
{
Privatejava. util. List <File> Uploads;
Privatejava. util. List <string> filenames;
Privatejava. util. List <string> uploadcontenttypes;
Publicjava. util. List <string> getuploadfilename ()
{
Returnfilenames;
}
Publicvoidsetuploadfilename (Java. util. List <string> filenames)
{
This. filenames = filenames;
}
Publicjava. util. List <File> getupload ()
{
Returnuploads;
}
Publicvoidsetupload (Java. util. List <File> uploads)
{
This. uploads = Uploads;
}
Publicvoidsetuploadcontenttype (Java. util. List <string> contenttypes)
{
This. uploadcontenttypes = contenttypes;
}
Publicjava. util. List <string> getuploadcontenttype ()
{
Returnthis. uploadcontenttypes;
}
Publicstringexecute () throwsexception
{
If (uploads! = NULL)
{
Inti = 0;
For (; I <uploads. Size (); I ++)
{
Java. Io. inputstreamis = newjava. Io. fileinputstream (uploads. Get (I ));
Java. Io. outputstreamos = newjava. Io. fileoutputstream (
"D: Upload" + filenames. Get (I ));
Bytebuffer [] = newbyte [8192];
Intcount = 0;
While (COUNT = is. Read (buffer)> 0)
{
OS. Write (buffer, 0, count );
}
OS. Close ();
Is. Close ();
}
}
Returnsuccess;
}
}
In the execute method, only list objects are enumerated. The code in the loop is basically the same as the code when uploading a single file. If you have used the struts1.x Upload Component, do you feel that the upload function of struts2 is easier to implement? When uploading multiple files in struts1.x, you must create an attribute with an index. In struts2. Figure 1 shows the interface for uploading any number of files.
{
Get_larger (this)
} "Src =" http://img.ddvip.com/2009_01_16/1232099224_ddvip_6646.jpg "alt =" struts2 tutorial 7: upload any number of files ">
Figure 1
This article provides a method to use struts2 to upload any number of files. If any reader has a better method, follow the instructions!