method One (file flow mode):
private static final int buffer_size = 16 * 1024;
Get Save Path
String basepath=request.getsession (). Getservletcontext (). Getrealpath ("/");//Get Project path
String dir=basepath+ "images/" +filedate+ "/";
Copy and upload files to the target file---upload files
private static void copy (file src, file dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
if (!dst.exists () | | | dst== null) {//Determines whether the specified path dir exists, does not exist, creates a path
Dst.mkdirs ();
}
in = new Bufferedinputstream (new FileInputStream (SRC),
Buffer_size);
out = new Bufferedoutputstream (new FileOutputStream (DST),
Buffer_size);
byte[] buffer = new Byte[buffer_size];
while (in.read (buffer) > 0) {
Out.write (buffer);
}
} finally {
if (null! = IN) {
In.close ();
}
if (null! = out) {
Out.close ();
}
}
} catch (Exception e) {
E.printstacktrace ();
}
}
Method Two (struts2 fileutils to upload-----individual files):
Form form in =========== page ===========
<form action= "${pagecontext.request.contextpath}/control/employee/list_execute.action" enctype= "multipart/ Form-data "method=" POST ">
File: <input type= "file" name= "image" >
<input type= "Submit" value= "Upload"/>
</form>
============ corresponding to Struts2 action==============
Import Java.io.File;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext;
public class Helloworldaction {
The name of the corresponding file on the private file image;//page
Private String imagefilename;//to get struts2 default file name (page file name +filename)
Public String Getimagefilename () {
return imagefilename;
}
public void Setimagefilename (String imagefilename) {
This.imagefilename = Imagefilename;
}
Public File GetImage () {
return image;
}
public void SetImage (File image) {
This.image = image;
}
Public String Execute () throws exception{//upload file method
String Realpath = Servletactioncontext.getservletcontext (). Getrealpath ("/images");//Get the absolute path to the images file
System.out.println (Realpath);
if (image!=null) {
File SaveFile = new file (new file (Realpath), imagefilename);
if (!savefile.getparentfile (). exists ()) Savefile.getparentfile (). Mkdirs ();
Fileutils.copyfile (image, SaveFile);
Actioncontext.getcontext (). Put ("message", "Upload succeeded");
}
Return "Success";
}
}
Note: <constant name= "struts.multipart.maxSize" value= "10701096"/> Configure the maximum uploaded file in Struts.xml, set 10M
Method Three (struts2 fileutils to upload-----multiple files):
Form form in =========== page ===========
<form action= "${pagecontext.request.contextpath}/control/employee/list_execute.action" enctype= "multipart/ Form-data "method=" POST ">
File 1:<input type= "file" name= "image" ><br/>
File 2:<input type= "file" name= "image" ><br/>
File 3:<input type= "file" name= "image" ><br/>
<input type= "Submit" value= "Upload"/>
</form>
============ corresponding to Struts2 action==============
Import Java.io.File;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext;
public class Helloworldaction {
Private file[] image;//the name of the corresponding file on the page---------------------and the individual file different points are defined as an array
Private string[] imagefilename;//This can get struts2 default file name (page file name +filename)-----------------and a single file different points are defined as an array
Public file[] GetImage () {
return image;
}
public void SetImage (file[] image) {
This.image = image;
}
Public string[] Getimagefilename () {
return imagefilename;
}
public void Setimagefilename (string[] imagefilename) {
This.imagefilename = Imagefilename;
}
Public String Execute () throws exception{
String Realpath = Servletactioncontext.getservletcontext (). Getrealpath ("/images");
System.out.println (Realpath);
if (image!=null) {
File Savedir = new file (Realpath);
if (!savedir.exists ()) savedir.mkdirs ();
for (int i = 0; i<image.length; i++) {
File SaveFile = new file (Savedir, imagefilename[i]);
Fileutils.copyfile (Image[i], savefile);
}
Actioncontext.getcontext (). Put ("message", "Upload succeeded");
}
Return "Success";
}
}