Struts2提供FileUpload攔截器和Jakarta Commons FileUpload組件以實現文件的上傳功能,即需要導入commons-fileupload-1.3.jar與commons-io-2.0.1.jar依賴包。
要想使用HTML表單上傳一個或多個文件,則需要:
將表單的method屬性設置為post;將表單的enctype屬性設置為multipart/form-data;添加<input type="file">字段或使用s:file標簽。第一步:使用s:file標簽,若需上傳多個文件則使用多個s:file標簽,并確保其name屬性值一致;核心示例代碼如下所示:
<s:form action="testUpload" enctype="Multipart/form-data" method="post"> <s:file name=" 第二步:在對應Action類中定義與文件上傳相關的三個基本屬性,提供其getter()和setter()方法;三個基本屬性分別是:PRivate File [fileFieldName]; // 被上傳的文件所對應的File對象private String [fileFieldName]ContentType; // 文件類型private String [fileFieldName]FileName; // 文件名第三步:使用IO流進行文件的上傳即可。核心示例代碼如下所示:
/** * 利用IO流實現文件的上傳 * 1). 獲取服務器的文件存放路徑 * 2). 準備輸入流、輸出流 * 3). 進行文件上傳 * 4). 關閉輸出流、輸入流 */ServletContext context = ServletActionContext.getServletContext();String realPath = context.getRealPath("/files/" + pptFileName);System.out.println(realPath);FileOutputStream out = new FileOutputStream(realPath);FileInputStream in = new FileInputStream(ppt);int len = 0;byte[] buffer = new byte[1024];while((len = in.read(buffer)) != -1) { out.write(buffer, 0, len);}注意:如何實現一次性上傳多個文件?將上述屬性改為List類型,且需保證多個文件域的name屬性值一致,但要特別注意文件描述字段的回顯問題。
可以通過配置FileUploadInterceptor 攔截器參數的方式來限制上傳文件的大小、類型和擴展名??膳渲脜等缦滤荆?/p>maximumSize (optional):單個上傳文件的最大值,默認為2M;allowedTypes (optional):所允許上傳文件的類型,多個類型間用“,”隔開;allowedExtensions (optional):所允許上傳文件的擴展名,多個擴展名間用“,”隔開;
攔截器配置示例如下所示:
<!-- 配置FileUpload攔截器:限制上傳文件的大小、類型和擴展名 --><interceptors> <interceptor-stack name="uploadControlStack"> <interceptor-ref name="defaultStack"> <param name="fileUpload.maximumSize">4000</param> <param name="fileUpload.allowedTypes">text/html,text/xml</param> <param name="fileUpload.allowedExtensions">html,xml</param> </interceptor-ref> </interceptor-stack></interceptors><default-interceptor-ref name="uploadControlStack"></default-interceptor-ref>注意:在org.apache.struts2下的default.properties屬性文件中對上傳文件總大小進行了限制,可以使用常量的方式來修改該限制。
可以在國際化資源文件中定義如下消息:
struts.messages.error.uploading:文件上傳出錯的消息;struts.messages.error.file.too.large:文件大小超出最大值的消息;struts.messages.error.content.type.not.allowed:文件類型不合法的消息;struts.messages.error.file.extension.not.allowed:文件擴展名不合法的消息注意:此種方式定制的消息并不完善,具體可參考org.apache.struts2下struts-messages.properties屬性文件, 以提供合理完善的錯誤消息。
在某些應用程序里, 可能需要動態地將一個文件發送到用戶的瀏覽器中,而該文件名和存放路徑在編程時時無法預知的,如導出數據表等。而Struts專門為文件下載提供了一種Stream結果類型,在使用一個Stream結果時,不必準備一個jsp頁面。
可以為type=”stream”的result設定如下參數:
contentType:結果類型;contentLength:所下載文件的長度;contentDisposition:設定Content-Dispositoin響應頭,指定響應為文件下載類型,通常設置為attachment;filename=document.pdf;inputName:指定Action類中提供的文件輸入流的getter對應的屬性名,默認為inputStream;bufferSize:緩存區大小,默認為1024;allowCaching:是否允許使用緩存,默認為true;contentCharSet:指定下載的字符集。 注意:以上參數可以在Action類中以getter()方法的方式提供。
// 文件下載對應的Action類package com.qiaobc.struts.action;import java.io.FileInputStream;import java.io.InputStream;import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class DownloadAction extends ActionSupport { private static final long serialVersionUID = 1L; private String contentType; private long contentLength; private String contentDisposition; private InputStream inputStream; public String getContentType() { return contentType; } public long getContentLength() { return contentLength; } public String getContentDisposition() { return contentDisposition; } public InputStream getInputStream() { return inputStream; } @Override public String execute() throws Exception { // 確定各個成員變量的值 contentType = "text/html"; contentDisposition = "attachment;filename=action-download.html"; ServletContext context = ServletActionContext.getServletContext(); String fileName = context.getRealPath("/files/action.html"); inputStream = new FileInputStream(fileName); contentLength = inputStream.available(); return super.execute(); }新聞熱點
疑難解答