1. Build a struts2 environment
Under myeclipse, right-click Project ->MyEclipse ->Project Facets ->install Apache Struts2.
If you want to build it yourself, you need to download the struts2 package and write the struts.xml configuration file.
The web.xml file configuration is as follows:
<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>*.action</url-pattern> </filter-mapping>
2. File upload
1. Front Desk Page:
Upload page:
<body> <form action="upload.action" method="post" enctype="multipart/form-data"> <input type="file" name="upload"/> <input type="submit" value="submit"/> <br> ${result} </form> </body>The input name attribute is consistent with the background naming.
Upload failed page:
<body> <h2>Upload failed</h2> <s:fielderror></s:fielderror> </body>
need:
<%@ taglib uri="/struts-tags" prefix="s"%>
2. Backend Action
Main properties upload, uploadContentType, uploadFileName.
package com.yf.action;import java.io.File;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport{ private File upload; private String uploadContentType; private String uploadFileName; private String result; public File getUoload() { 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 getResult() { return result; } public void setResult(String result) { this.result = result; } @Override public String execute() throws Exception { String path = ServletActionContext.getServletContext().getRealPath("/images"); File file = new File(path); if(!file.exists()){ file.mkdir(); } System.out.println(upload); FileUtils.copyFile(upload, new File(file,uploadFileName)); result = "Uploaded successfully"; return SUCCESS; }}3.struts.xml file configuration
Configure action and configure interceptor to limit the type and size of uploaded files.
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.devMode" value="true"/> <constant name="struts.multipart.saveDir" value="/tmp"/> <constant name="struts.custom.i18n.resources" value="app"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="upload"> <result>/index.jsp</result> <result name="input">/error.jsp</result> <!-- Configure interceptor to limit the type and size of uploaded files--> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpeg</param> <param name="maximumSize">2M</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package></struts>
4. Create a new properties file
The file upload failed information is displayed to the foreground, and the error message is displayed when processing.
The file contents are as follows:
struts.messages.error.file.too/large=/u4E0A/u4F20/u6587/u4EF6/u592A/u5927/u4E86/uFF01struts.messages.error.content.type.not.allowed=/u4E0A/u4F20/u6587/u4EF6/u7C7B/u578B/u4E0D/u7B26/uFF01
That is to add:
Name: struts.messages.error.file.too/large
value: The upload file is too large!
Name : struts.messages.error.content.type.not.allowed
value: The uploaded file type does not match!
The operation results are as follows:
Select jpg image, the size does not exceed 2M, after running
Select a non-picture file:
If you need to upload files in batches, change the background upload, uploadContentType, and uploadFileName to List, read the upload file in a loop and save it to the hard disk, add input to the front desk, and the name attribute is the same.
The above is an example analysis of the Struts2 file upload function introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!