Simulate upload of a single file in two ways. The effect is as follows:
The development procedure is as follows:
1. Create a new web project and import the jar required for struts2 file upload, as shown in figure
Directory structure
2. Create an Action
Method 1
Package com. ljq. action; import java. io. file; import org. apache. commons. io. fileUtils; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport; @ SuppressWarnings ("serial") public class UploadAction extends ActionSupport {private File image; // the uploaded File private String imageFileName; // the File name private String imageContentType; // file type p Ublic String execute () throws Exception {String realpath = ServletActionContext. getServletContext (). getRealPath ("/images"); // D: \ apache-tomcat-6.0.18 \ webapps \ struts2_upload \ images System. out. println ("realpath:" + realpath); if (image! = Null) {File savefile = new File (realpath), imageFileName); if (! Savefile. getParentFile (). exists () savefile. getParentFile (). mkdirs (); FileUtils. copyFile (image, savefile); ActionContext. getContext (). put ("message", "File Uploaded successfully") ;}return "success" ;}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 getImageContentType () {return imageContentType;} public void setImageContentType (String imageContentType) {this. imageContentType = imageContentType ;}}
Method 2
Package com. ljq. action; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionSupport; @ SuppressWarnings ("serial") public class UploadAction2 extends ActionSupport {// encapsulate the private File image attribute of the uploaded File domain; // encapsulate the private String imageContentType attribute of the uploaded File type; // encapsulate the property pr of the uploaded file name Ivate String imageFileName; // The private String savePath that accepts dependency injection; @ Override public String execute () {FileOutputStream fos = null; FileInputStream FD = null; try {// create the file output stream System. out. println (getSavePath (); fos = new FileOutputStream (getSavePath () + "\" + getImageFileName ()); // create a file upload stream. The file is uploaded to the new FileInputStream (getImage (); byte [] buffer = new byte [1024]; int len = 0; while (len = Fi. read (Buffer)> 0) {fos. write (buffer, 0, len) ;}} catch (Exception e) {System. out. println ("File Upload Failed"); e. printStackTrace () ;}finally {close (fos, FCM) ;}return SUCCESS ;}/ *** return the location where the uploaded file is saved ** @ return */public String getSavePath () throws Exception {return ServletActionContext. getServletContext (). getRealPath (savePath);} public void setSavePath (String savePath) {this. savePath = savePath;} public F Ile getImage () {return image;} public void setImage (File image) {this. image = image;} public String getImageContentType () {return imageContentType;} public void setImageContentType (String imageContentType) {this. imageContentType = imageContentType;} public String getImageFileName () {return imageFileName;} public void setImageFileName (String imageFileName) {this. imageFileName = imageFile Name;} private void close (FileOutputStream fos, FileInputStream FAS) {if (FS! = Null) {try {FS. close ();} catch (IOException e) {System. out. println ("failed to disable FileInputStream"); e. printStackTrace () ;}} if (fos! = Null) {try {fos. close ();} catch (IOException e) {System. out. println ("failed to disable FileOutputStream"); e. printStackTrace ();}}}}
Struts. xml configuration file
/WEB-INF/page/message. jsp
/Images
/WEB-INF/page/message. jsp
/Upload. jsp
Image/bmp, image/png, image/gif, image/jpeg
1025956
Upload form page
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri ="/struts-tags "prefix =" s "%> File Upload
Display result page
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri ="/struts-tags "prefix =" s "%> Uploaded
Uploaded successfully!
">