国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Struts2-14 文件的上傳與下載

2019-11-08 02:56:27
字體:
來源:轉載
供稿:網友

  Struts2提供FileUpload攔截器和Jakarta Commons FileUpload組件以實現文件的上傳功能,即需要導入commons-fileupload-1.3.jar與commons-io-2.0.1.jar依賴包。


一、文件的上傳

1.1 表單準備

 要想使用HTML表單上傳一個或多個文件,則需要:

將表單的method屬性設置為post;將表單的enctype屬性設置為multipart/form-data;添加<input type="file">字段或使用s:file標簽。

1.2 實現步驟

 第一步:使用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屬性值一致,但要特別注意文件描述字段的回顯問題。  

1.3 配置FileUpload攔截器

 可以通過配置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屬性文件中對上傳文件總大小進行了限制,可以使用常量的方式來修改該限制。  

1.4 定制錯誤消息

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

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頁面。

2.1 具體實現

Struts2中使用type=”stream”的result進行下載;具體使用細節參看struts-2.3.15.3-all/struts-2.3.15.3/docs/WW/docs/stream-result.html。<action name="testDownload" class="com.qiaobc.struts.action.DownloadAction" method="execute"> <result type="stream"> <!--靜態指定屬性值--> <param name="bufferSize">2048</param> </result></action>

2.2 參數配置

 可以為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(); }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 象州县| 凤台县| 当雄县| 虞城县| 银川市| 仁布县| 射阳县| 万盛区| 桑植县| 玛纳斯县| 忻州市| 西乌| 丁青县| 洪雅县| 友谊县| 塔河县| 濮阳市| 保山市| 南陵县| 阳东县| 墨脱县| 乌鲁木齐县| 舒城县| 郎溪县| 会昌县| 南召县| 巨鹿县| 福清市| 钟山县| 临清市| 乌恰县| 财经| 江陵县| 乐亭县| 清远市| 揭东县| 黄浦区| 昌图县| 舟山市| 凌源市| 泸定县|