在Struts2, <s:file> 標(biāo)簽用于創(chuàng)建一個(gè)HTML文件上傳組件,允許用戶從本地磁盤(pán)選擇文件,并將其上傳到服務(wù)器。在本教程中,您將創(chuàng)建與文件上傳組件JSP頁(yè)面,設(shè)置最大大小和允許上傳文件的內(nèi)容類型,并顯示上傳文件的詳細(xì)信息。
這里創(chuàng)建一個(gè)Web工程:strut2uploadfile,來(lái)演示在多個(gè)復(fù)選框如何設(shè)置的默認(rèn)值,整個(gè)項(xiàng)目的結(jié)構(gòu)如下圖所示:
Action類的文件上傳,聲明“File”變量來(lái)存儲(chǔ)用戶上傳的文件,兩個(gè)字符串變量以存儲(chǔ)文件名和內(nèi)容類型。“文件上傳攔截器”通過(guò)設(shè)置“X”的ContentType(),并設(shè)置“X”FileName()會(huì)自動(dòng)注入上傳的文件細(xì)節(jié),確保方法名拼寫(xiě)正確。
P.S X是以存儲(chǔ)上傳的文件中的變量。
FileUploadAction.java
package com.yiibai.common.action; import java.io.File; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport{ private File fileUpload; private String fileUploadContentType; private String fileUploadFileName; public String getFileUploadContentType() { return fileUploadContentType; } public void setFileUploadContentType(String fileUploadContentType) { this.fileUploadContentType = fileUploadContentType; } public String getFileUploadFileName() { return fileUploadFileName; } public void setFileUploadFileName(String fileUploadFileName) { this.fileUploadFileName = fileUploadFileName; } public File getFileUpload() { return fileUpload; } public void setFileUpload(File fileUpload) { this.fileUpload = fileUpload; } public String execute() throws Exception{ return SUCCESS; } public String display() { return NONE; } }
使用<s:file>標(biāo)簽來(lái)渲染一個(gè)文件上傳組件,并設(shè)置表單的enctype類型為:“multipart/form-data”。
fileupload.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <s:head /> </head> <body> <h1>Struts 2 <s:file> file upload example</h1> <s:form action="resultAction" namespace="/" method="POST" enctype="multipart/form-data"> <s:file name="fileUpload" label="Select a File to upload" size="40" /> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 <s:file> file upload example</h1> <div><div class="ads-in-post hide_if_width_less_800"> <script async src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- 728x90 - After2ndH4 --> <ins class="adsbygoogle hide_if_width_less_800" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-2836379775501347" data-ad-slot="3642936086" data-ad-region="yiibairegion"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></div><h2> File Name : <s:property value="fileUploadFileName"/> </h2> <h2> Content Type : <s:property value="fileUploadContentType"/> </h2> <h2> File : <s:property value="fileUpload"/> </h2> </body> </html>
<?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.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="global" /> <package name="default" namespace="/" extends="struts-default"> <action name="fileUploadAction" class="com.yiibai.common.action.FileUploadAction" method="display"> <result name="none">/pages/fileupload.jsp</result> </action> <action name="resultAction" class="com.yiibai.common.action.FileUploadAction"> <interceptor-ref name="exception"/> <interceptor-ref name="i18n"/> <interceptor-ref name="fileUpload"> <param name="allowedTypes">text/plain</param> <param name="maximumSize">10240</param> </interceptor-ref> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <result name="success">/pages/result.jsp</result> <result name="input">/pages/fileupload.jsp</result> </action> </package> </struts>
文件大小限制
在這個(gè)例子中,您將通過(guò)“文件上傳攔截”上傳文件大小的限制, 該值以字節(jié)為單位計(jì)數(shù)。在本實(shí)例中,上載文件的最大尺寸是10KB。
文件類型
可以通過(guò)設(shè)置“文件上傳攔截器”允許的文件類型。在這種情況下,上傳文件只接受“text/plain”的類型。
http://localhost:8080/Struts2Example/fileUploadAction.action
錯(cuò)誤信息提示,如果您上傳一個(gè)文件,該文件超過(guò)10KB,或者未選文本文件。
上傳名為“yiibai.com.txt”的文本文件, 文件大小 : 5kb.
上傳的文件將被視為一個(gè)臨時(shí)文件,具有長(zhǎng)的隨機(jī)文件名,如:upload__376584a7_12981122379__8000_00000010.tmp. 請(qǐng)確保這個(gè)臨時(shí)文件復(fù)制到其他地方。 閱讀文件實(shí)用文檔復(fù)制文件。
下載代碼 – http://pan.baidu.com/s/1eQDH07S