<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!--設置字符編碼集,能夠避免文件名亂碼--> <constant name="struts.i18n.encoding" value="GB18030" /> <!--設置上傳文件的最大容許大小--> <constant name="struts.multipart.maxSize" value="2097152000" /> <!-- 當struts.xml配置文件發生修改,會馬上加載,在生產環境下最好不要配置 --> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 會提供更加友好的提示信息 --> <constant name="struts.devMode" value="true"/> <!-- 須要繼承struts-default包,這樣就擁有的最基本的功能 --> <package name="upload-package" extends="struts-default"> <action name="upload" class="com.bjpowernode.struts2.UploadTestAction"> <result>/success.jsp</result> </action> </package> </struts>
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> </head> <body> <form action="upload.action" method="post" enctype="multipart/form-data"> 標題:<input type="text" name="title"><br> 文件:<input type="file" name="myFile"><br> <input type="submit" value="上傳"> </form> </body> </html>
取得內容類型->>規則:輸入域的名稱+固定字符串ContentTypehtml
package com.bjpowernode.struts2; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import com.opensymphony.xwork2.Action; public class UploadTestAction { private String title; //能夠獲得上傳文件的名稱 //規則:輸入域的名稱+固定字符串FileName private String myFileFileName; //取得文件數據 //規則:File 輸入域的名稱 private File myFile; // //取得內容類型 // //規則:輸入域的名稱+固定字符串ContentType // private String myFileContentType; public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } // public String getMyFileContentType() { // return myFileContentType; // } // // // public void setMyFileContentType(String myFileContentType) { // this.myFileContentType = myFileContentType; // } // public String execute() throws Exception { InputStream is = null; OutputStream os = null; try { is = new BufferedInputStream( new FileInputStream(myFile)); os = new BufferedOutputStream( new FileOutputStream("c:\\" + myFileFileName)); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) > 0) { os.write(buffer, 0, len); } }finally { if (is != null) {is.close();} if (os != null) {os.close();} } return Action.SUCCESS; } }七、在Action中提供getter和setter方法,便於收集數據(這樣收集數據的模式通常能夠成爲屬性驅動模式) 八、將JSP和Action配置到struts.xml配置文件中 九、瞭解struts-default.xml配置文件,default.properties,Struts2的默認後綴爲.action