For how to upload files under the Struts2 framework, the struts2 framework
Struts2 configuration process
(1) Add a jar package to the Project
(2) filter configuration in web. xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
(3) Compiling the struts. xml configuration file
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
(4) write the action class
Package com. xmgc. SC. action; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import org. apache. struts2.ServletActionContext; public class MyUpLoadAction {private String title; private File upload; // The name of the file type in the form is the same as that of the private String uploadContentType; // The prefix must be the above uploadprivate String uploadFileName; public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public File getUpload () {return upload;} public void setUpload (File upload) {this. upload = upload;} public String getUploadContentType () {return uploadContentType;} public void setUploadContentType (String uploadContentType) {this. uploadContentType = uploadContentType;} public String getUploadFileName () {return uploadFileName;} public void setUploadFileName (String uploadFileName) {this. uploadFileName = uploadFileName;}/* public String getSavePath () {// ServletContext cxt = ServletActionContext. getServletContext (); // String path = cxt. getRealPath ("/"); // The obtained path is: http: // localhost: 8080/SC // After uploading, it becomes E: \ software \ apache-tomcat-6.0.45 \ webapps \ screturn savePath;} public void setSavePath (String savePath) {// E: \ software \ apache-tomcat-6.0.45 \ webapps \ SC/myuploadthis. savePath = ServletActionContext. getServletContext (). getRealPath ("/myupload");} */public String execute () throws IOException {System. out. println (title); // The title System. out. println (uploadContentType); // file type of the file to be uploaded: System. out. println (uploadFileName); // file name of the file to be uploaded. Remember that the file extension is System. out. println (upload); String realPath = ServletActionContext. getServletContext (). getRealPath ("/"); String path = realPath + "myupload/" + uploadFileName; System. out. println (realPath); System. out. println (path); FileInputStream FCM = new FileInputStream (upload); FileOutputStream fos = new FileOutputStream (path); byte [] bytes = new byte [1024]; // define a 1024-size byte array int len =-1; // used for flag while (len = Fi. read (bytes)> 0) {fos. write (bytes, 0, len);} return null ;}}
(5) Compiling JSP pages
<% @ Page contentType = "text/html; charset = UTF-8" %> <! -- Enctype = "multipart/form-data", which is the most important configuration --> <form action = "myupload. action "enctype =" multipart/form-data "method =" post "> file name: <input type =" text "name =" title "/> <br/> upload: <input type = "file" name = "upload"/> <br/> <input type = "submit" value = "upload"/> </form>
After this conclusion, it seems that the upload of a single file under the struts2 framework is still very simple. You only need to obtain the address where the file is to be stored and then input and output the stream, write it to this address. Most of the work has been done by struts2.