Struts2提供了文件上傳的框架,Struts2默認的文件上傳框架是cos,你們也能夠根據本身的須要修改Struts2中的文件上傳框架,對於文件上傳來講,不管使用Struts2中的哪種框架,上傳方式都是比較簡單的。下面用一個案例介紹Struts2的文件上傳。html
該案例比較簡單,容許用戶進行文件上傳,上傳成功提示文件上傳的主題,上傳失敗則提示上傳失敗的提示,固然爲了防止惡意上傳,我專門作了一個簡單的文件過濾,限制文件的格式java
首先看頁面的操做apache
上傳成功的狀況數組
上傳失敗的狀況框架
接下來分析源碼jsp
uploadFile.jsp頁面ide
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上傳</title>
</head>
<body>
<s:form action="upload" enctype="multipart/form-data">
<s:textfield name="title">文件標題</s:textfield>
<s:file name="upload" label="選擇文件"></s:file>
<s:submit value="上傳"></s:submit>
</s:form>
</body>
</html>
上傳文件對應的Actionui
package test.Action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class fileUploadAction extends ActionSupport { private String title; private File upload; private String uploadContentType; private String uploadFileName; private String savePath; // 設置文件格式 private String allowType; public String getAllowType() { return allowType; } public void setAllowType(String allowType) { this.allowType = allowType; } 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() { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } @Override public String execute() throws Exception { // 獲取文件內容 FileInputStream fin = new FileInputStream(getUpload()); // 定義一個byte數組 byte[] buffer = new byte[1024]; // 創建文件輸出流,創建文件位置 FileOutputStream fout = new FileOutputStream(getSavePath() + "\\" + getUploadFileName()); // 每讀完1M的量就往文件裏寫 int len = 0; while ((len = fin.read(buffer)) > 0) { fout.write(buffer, 0, len); } System.out.println("fileUploadAction [title=" + title + ", upload=" + upload + ", uploadContentType=" + uploadContentType + ", uploadFileName=" + uploadFileName + ", savePath=" + savePath + "]"); return SUCCESS; } public String filterFileType(String[] types) { // 獲取文件的格式 String fileType = this.getUploadContentType(); System.out.println(fileType); if (fileType != null) { for (String Type : types) { if (Type.equals(fileType)) { return null; } else return ERROR; } } return ERROR; } @Override public void validate() { //接收驗證結果 String Result=this.filterFileType(getAllowType().split(",")); if(Result!=null) { addFieldError("uploadError", "該文件格式 不正確"); } } }
Struts.xml代碼this
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="upload" extends="struts-default"> <action name="upload" class="test.Action.fileUploadAction"> <param name="savePath">/WEB-INF/uploadFiles</param> <param name="allowType">image/x-png,</param> <result>/success.jsp</result> <result name="input">/uploadError.jsp</result> </action> </package> </struts>
分別對應的成功和失敗兩個jsp 視圖spa
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>結果</title> </head> <body> 上傳成功 文件標題:<s:property value="title" /><br> 文件爲:<s:property value="upload"/> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>更新失敗</title>
</head>
<body>
<s:fielderror name="uploadError"></s:fielderror>
</body>
</html>