26.struts2_文件的上傳


一、準備工作

表單準備:將 HTML表單 enctype屬性設置爲 multipart/form-data

  表單需要使用 method爲post的方式

   添加<input type=“file”>字段

二、struts2對文件上傳的支持

框架中,FileUpload攔截器和Jakarta Commons FileUpload組件可以完成文件的上傳。

步驟:

1.在對應Action建立三個屬性

private File [fileFieldName]; //這個fileFieldName對應頁面上<s:file name=""/> 的name屬性

private String  [fileFieldName]ContentType; 

private String [fileFieldName]FileName;

UploadAction.java


public class UploadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File ppt;
	
	private String  pptContentType; 
	
	private String pptFileName;

	private String pptDesc;
	
	
	
	public String getPptDesc() {
		return pptDesc;
	}

	public void setPptDesc(String pptDesc) {
		this.pptDesc = pptDesc;
	}

	public File getPpt() {
		return ppt;
	}

	public void setPpt(File ppt) {
		this.ppt = ppt;
	}

	public String getPptContentType() {
		return pptContentType;
	}

	public void setPptContentType(String pptContentType) {
		this.pptContentType = pptContentType;
	}

	public String getPptFileName() {
		return pptFileName;
	}

	public void setPptFileName(String pptFileName) {
		this.pptFileName = pptFileName;
	}
	

	@Override
	public String execute() throws Exception {
		
		System.out.println(ppt);
		System.out.println(pptContentType);
		System.out.println(pptFileName);
		System.out.println(pptDesc);
		
		//使用IO流進行上傳
		
		ServletContext sac=ServletActionContext.getServletContext();
		String dir=sac.getRealPath("/files/"+pptFileName);
		
		System.out.println(dir);
		
		FileOutputStream out =new FileOutputStream(dir);
		FileInputStream in = new FileInputStream(ppt);
		
		byte[] buffer=new byte[1024];
		int len=0;
		
		while((len=in.read(buffer))!=-1){
			out.write(buffer,0,len);
			
		}
		out.close();
		in.close();
		
		return SUCCESS;
	}
}


upload.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<s:form action="testUpload" method="post" enctype="multipart/form-data">
		<s:file name="ppt" label="PPTFile">
		</s:file>
		<s:textfield name="pptDesc" label="PPTDesc" ></s:textfield>
		<s:submit></s:submit>
	</s:form>
</body>
</html>

三、配置FileUploadInterceptor攔截器參數,限制上傳,並給出提示


  • maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this isnot related to the various properties found in struts.properties. Default to approximately 2MB.//單個文件上傳最大值

  • allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all types to be uploaded.//允許的上傳文件類型,多個使用【,】分隔

  • allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all extensions to be uploaded.//允許上傳文件的擴展名,逗號分隔

可以在國際化資源中定義如下消息

  • struts.messages.error.uploading - 文件上傳出錯

  • struts.messages.error.file.too.large - 超過大小

  • struts.messages.error.content.type.not.allowed - 文件內容類型不合法

  • struts.messages.error.file.extension.not.allowed - 文件擴展名不合法
添加錯誤提示後的struts.xml

<?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.custom.i18n.resources" value="i18n"></constant>

	<package name="default" namespace="/" extends="struts-default">

	<!-- 配置攔截器棧 -->
		<interceptors>
			<interceptor-stack name="hcx">
				<interceptor-ref name="defaultStack">
					<param name="fileUpload.maximumSize">2000</param>
					<param name="fileUpload.allowedTypes">text/plain,text/xml</param>
					<param name="fileUpload.allowedExtensions">txt,html</param>
				</interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<!-- 使用攔截器棧 -->
		<default-interceptor-ref name="hcx"/> 

		<action name="testUpload" class="com.hcx.app.UploadAction">
			<result name="success">/success.jsp</result>
			<result name="input">/upload.jsp</result>
		</action>

	</package>
</struts>

但是目前提示消息不完善,具體可以參考org.apache.struts2下的struts-messages.properties文件,下面是例子中的國際化文件

struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}


注意: 在struts2的defaul.properties中有對上傳文件總的大小限制,爲2M。struts.multipart.maxSize=2097152。如果修改可以按照上述方式。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章