web.xml:html
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 7 <welcome-file-list> 8 <welcome-file>index.jsp</welcome-file> 9 </welcome-file-list> 10 11 <filter> 12 <filter-name>struts2</filter-name> 13 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 14 </filter> 15 16 <filter-mapping> 17 <filter-name>struts2</filter-name> 18 <url-pattern>/*</url-pattern> 19 </filter-mapping> 20 21 </web-app>
index.jsp:java
1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 6 <title>Insert title here</title> 7 </head> 8 <body> 9 <s:fielderror></s:fielderror> 10 <form action="upload" method="post" enctype="multipart/form-data"> 11 <input type="file" name="file"><br> 12 <input type="submit" value="上傳"> 13 </form> 14 </body> 15 </html>
struts.xml:web
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <constant name="struts.custom.i18n.resources" value="mess"></constant> 9 <package name="default" extends="struts-default"> 10 <action name="upload" class="com.action.FileUploadAction"> 11 <interceptor-ref name="fileUpload"> 12 <!-- 只准許上傳圖片文件 --> 13 <param name="allowedTypes">image/png,image/gif,image/jpeg</param> 14 <!-- 圖片最大不能超過50000字節 --> 15 <param name="maximumSize">5000</param> 16 </interceptor-ref> 17 <interceptor-ref name="defaultStack"/> 18 <param name="savePath">/upload</param> 19 <result name="success">/WEB-INF/success.jsp</result> 20 <result name="input">/index.jsp</result> 21 </action> 22 </package> 23 24 </struts>
FileUploadAction.java:apache
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 5 import org.apache.struts2.ServletActionContext; 6 7 import com.opensymphony.xwork2.ActionSupport; 8 9 /** 10 *@date 2014-4-18 11 */ 12 public class FileUploadAction extends ActionSupport{ 13 14 private File file; 15 private String fileFileName; 16 private String fileContentType; 17 private String savePath; //在struts.xml中配置的屬性 18 public File getFile() { 19 return file; 20 } 21 public void setFile(File file) { 22 this.file = file; 23 } 24 public String getFileFileName() { 25 return fileFileName; 26 } 27 public void setFileFileName(String fileFileName) { 28 this.fileFileName = fileFileName; 29 } 30 public String getFileContentType() { 31 return fileContentType; 32 } 33 public void setFileContentType(String fileContentType) { 34 this.fileContentType = fileContentType; 35 } 36 public String getSavePath() { 37 return savePath; 38 } 39 public void setSavePath(String savePath) { 40 this.savePath = savePath; 41 } 42 43 @Override 44 public String execute() throws Exception { 45 ServletActionContext.getRequest().setAttribute("filename",this.getFileFileName()); 46 String webInfoPath=ServletActionContext.getServletContext().getRealPath("/WEB-INF/"); 47 FileOutputStream fos=new FileOutputStream(webInfoPath+this.getSavePath()+File.separator+this.getFileFileName()); 48 FileInputStream fis=new FileInputStream(this.getFile()); 49 byte[] buffer=new byte[1024]; 50 int len=0; 51 while((len=fis.read(buffer))>0){ 52 fos.write(buffer,0,len); 53 } 54 return "success"; 55 } 56 57 }
success.jsp:app
1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 ${filename}上傳成功... 9 </body> 10 </html>
mess_zh_CN.properties:jsp
1 struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u4E0D\u88AB\u5141\u8BB8 2 struts.messages.error.file.too.large=\u4E0A\u4F20\u7684\u6587\u4EF6\u4F53\u79EF\u8FC7\u5927 3 struts.messages.error.uploading=\u4E0A\u4F20\u6587\u4EF6\u8FC7\u7A0B\u4E2D\u51FA\u73B0\u672A\u77E5\u9519\u8BEF
struts.messages.error.content.type.not.allowed=上傳的文件類型不被容許ide
struts.messages.error.file.too.large=上傳的文件體積過大post
struts.messages.error.uploading=上傳文件過程當中出現未知錯誤ui