This article shares with you the method of implementing file upload of Struts2 framework for your reference. The specific content is as follows
Struts2 configuration process
(1) Add jar package to the project
(2) Configuration of filter (filter) 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 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) Writing 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/javaeehttp://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) Writing of 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 file type in the form form is the same private String uploadContentType;//The prefix must be the above upload private 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("/"); //This gets path is: http://localhost:8080/sc //After uploading, it becomes E:/software/apache-tomcat-6.0.45/webapps/sc return savePath; } public void setSavePath(String savePath) { //E:/software/apache-tomcat-6.0.45/webapps/sc/myupload this.savePath = ServletActionContext.getServletContext().getRealPath("/myupload"); }*/ public String execute() throws IOException{ System.out.println(title);//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 there is also the extension System.out.println(upload); String realPath=ServletActionContext.getServletContext().getRealPath("/"); String path=realPath+"myupload/"+uploadFileName; System.out.println(realPath); System.out.println(path); FileInputStream fis=new FileInputStream(upload); FileOutputStream fos=new FileOutputStream(path); byte[] bytes=new byte[1024];//Define a byte array of 1024 size int len=-1;//Use to be used as flag bits while((len=fis.read(bytes))>0){ fos.write(bytes, 0, len); } return null; }}(5) Writing of JSP pages
<%@ page contentType="text/html;charset=utf-8"%> <!-- enctype="multipart/form-data", this 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 summary, I feel that uploading a single file under the struts2 framework is still very simple. Just get the address to be stored in, and then write it to this address through the input and output stream. For most of the work, struts2 has done it for us.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.