StrutsFileUpload
文件上傳的簡單范例
HTML
HTML頁面需要做兩件事情,首先,表單需要指定enctype="multipart/form-dataand",其次需要一個(gè)類型為file的<input>表單控件。
?? <form name="myForm" method="post"
???????? action="/mywebapp/uploadMyFile.do"
???????? enctype="multipart/form-data">
???????? Select File: <input type="file" name="myFile">????????? </br>
???????? <input type="submit" value="Upload File">
?? </form>
JSP
上面的HTML標(biāo)簽用Struts標(biāo)簽代替就是以下代碼:
?? <html:form action="/uploadMyFile.do"?????????????? enctype="multipart/form-data">
???????? Select File: <html:file property="myFile"> </br>
???????? <html:submit value="Upload File"/>
?? </html:form>
ActionForm
這個(gè)ActionForm需要一個(gè)FormFile類型的字段。
一般的ActionForm
??? import org.apache.struts.upload.FormFile;
??? public class MyActionForm extends ActionForm {
?????? private FormFile myFile;
?????? public void setMyFile(FormFile myFile) {
?????????? this.myFile = myfile;
?????? }
?????? public FormFile getMyFile() {
?????????? return myFile;
?????? }
??? }
動(dòng)態(tài)ActionForms
在struts-config.xml文件中寫上:
?? <form-bean name="myForm"???? type="org.apache.struts.action.DynaActionForm">
?????? <form-property name="myFile"???????? type="org.apache.struts.upload.FormFile"/>
?? </form-bean>
在Action中需要怎么寫呢?
其實(shí)沒什么特殊的,就象和得到其他屬性一樣,從ActionForm中得到FormFile屬性,得到后可以隨意進(jìn)行處理。比如我們可以從FileForm中得到文件名,文件大小,文件內(nèi)容
??? public ActionForward execute(ActionMapping mapping,
???????????????????????????????? ActionForm form,
???????????????????????????????? HttpServletRequest request,
???????????????????????????????? HttpServletResponse response)
???????????????????????????????? throws Exception {
??????? MyActionForm myForm = (MyActionForm)form;
??????? // Process the FormFile
??????? FormFile myFile = myForm.getMyFile();
??????? String contentType = myFile.getContentType();
??????? String fileName??? = myFile.getFileName();
??????? int fileSize?????? = myFile.getFileSize();
??????? byte[] fileData??? = myFile.getFileData();
??????? ...
??? }
文件上傳的配置
在struts-config.xml的<controller>element中可以設(shè)置如下參數(shù)來配置文件上傳:
bufferSize - 處理文件上傳的緩沖區(qū)大小,單位是字節(jié)。
默認(rèn)是4096byte。
maxFileSize - 允許上傳文件的大小。可以使用K,M,G為單位。
默認(rèn)是250M。
multipartClass - muiltpart請求處理器類的全局標(biāo)識(shí)名。默認(rèn)是org.apache.struts.upload.CommonsMultipartRequestHandler
tempDir - 處理文件上傳的臨時(shí)目錄。
還有一種可選的文件上傳插件的方式可提供使用,那就是實(shí)現(xiàn)
org.apache.struts.upload.MultipartRequestHandler接口。
可以在struts-config.xml的<controller>的multipartClass
來指定這個(gè)實(shí)現(xiàn)給接口的類。
====================================
StrutsFileDownload
Struts 1.2.6中推出了新的DownloadAction,用來簡化下載操作。
實(shí)現(xiàn)DownloadAction
我們需要擴(kuò)展org.apache.struts.actions.DownloadAction并實(shí)現(xiàn)
getStreamInfo()方法。如果我們要更改默認(rèn)的緩沖大小,我們也可以覆蓋
getBufferSize()方法。?
實(shí)現(xiàn)getStreamInfo() 方法
getStreamInfo() 方法返回一個(gè)StreamInfo對(duì)象- 它是DownloadAction類的內(nèi)
部類,其實(shí)是個(gè)內(nèi)部接口。DownloadAction為這個(gè)接口提供了兩個(gè)具體的靜態(tài)內(nèi)
部實(shí)現(xiàn)類:
FileStreamInfo - 簡化從磁盤系統(tǒng)下載文件。需要連同content type傳入一個(gè)java.io.File對(duì)象到構(gòu)造方法中。
ResourceStreamInfo - 簡化從web應(yīng)用資源下載文件。需要傳入ServletContext,路徑以及content type 到它的構(gòu)造方法中。
在下面的例子中,我們還提供了一個(gè)以Byte array方法實(shí)現(xiàn)StreamInfo接口的代
碼。
實(shí)現(xiàn)getBufferSize() 方法
DownloadAction默認(rèn)返回4096byte的緩沖區(qū)我們可以覆蓋這個(gè)方法來自定義用
來傳輸文件的緩沖區(qū)大小
范例
下面有三個(gè)例子:
使用文件
使用web應(yīng)用資源
使用byte array
FileStreamInfo范例
DownloadAction使用文件的例子。這個(gè)范例從struts-config.xml的action
mapping的parameter屬性來得到文件名。
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
public class ExampleFileDownload extends DownloadAction{
??? protected StreamInfo getStreamInfo(ActionMapping mapping,
?????????????????????????????????????? ActionForm form,
?????????????????????????????????????? HttpServletRequest request,
?????????????????????????????????????? HttpServletResponse response)
??????????? throws Exception {
??????? // Download a "pdf" file - gets the file name from the
??????? // Action Mapping's parameter
??????? String contentType = "application/pdf";
??????? File file????????? = new File(mapping.getParameter());
??????? return new FileStreamInfo(contentType, file);
??? }
}
ResourceStreamInfo范例
DownloadAction使用web應(yīng)用資源的范例。這個(gè)范例從struts-config.xml的
action mapping的parameter屬性來得到web應(yīng)用資源的路徑。
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
public class ExampleResourceDownload extends DownloadAction {
??? protected StreamInfo getStreamInfo(ActionMapping mapping,
?????????????????????????????????????? ActionForm form,
?????????????????????????????????????? HttpServletRequest request,
?????????????????????????????????????? HttpServletResponse response)
??????????? throws Exception {
??????? // Download a "jpeg" file - gets the file name from the
??????? // Action Mapping's parameter
??????? String contentType???????? = "image/jpeg";
??????? String path??????????????? = mapping.getParameter();
??????? ServletContext application = servlet.getServletContext();
??????? return new ResourceStreamInfo(contentType, application, path);
??? }
}
Byte Array 范例
DownloadAction使用字節(jié)數(shù)組(byte array)的范例。
這個(gè)例子創(chuàng)建了一個(gè)實(shí)現(xiàn)了StreamInfo接口的ByteArrayStreamInfo內(nèi)部類。
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
public class ExampleByteArrayDownload extends DownloadAction {
??? protected StreamInfo getStreamInfo(ActionMapping mapping,
?????????????????????????????????????? ActionForm form,
?????????????????????????????????????? HttpServletRequest request,
?????????????????????????????????????? HttpServletResponse response)
??????????? throws Exception {
??????? // Download a "pdf" file
??????? String contentType = "application/pdf";
??????? byte[] myPdfBytes? = null;// Get the bytes from somewhere
??????? return new ByteArrayStreamInfo(contentType, myPdfBytes);
??? }
??? protected class ByteArrayStreamInfo implements StreamInfo {
??????? protected String contentType;
??????? protected byte[] bytes;
??????? public ByteArrayStreamInfo(String contentType, byte[] bytes) {
??????????? this.contentType = contentType;
??????????? this.bytes = bytes;
??????? }
??????? public String getContentType() {
??????????? return contentType;
??????? }
??????? public InputStream getInputStream() throws IOException {
??????????? return new ByteArrayInputStream(bytes);
??????? }
??? }
}
在WEB頁面上使用DownloadAction
最大的疑惑是我么如何使用這個(gè)Action?
需要做兩件事情:
和任何Struts的action一樣,需要在struts-config.xml中進(jìn)行配置。
在WEB頁面中使用它對(duì)文件進(jìn)行連接
下面是struts-config.xml配置的一個(gè)例子:
??? <action path="/downloadMyPdfFile" type="myPackage.ExampleFileDownload"
????? parameter="/foo/bar.pdf">
??? <action path="/downloadMyImage"?? type="myPackage.ExampleResourceDownload"
????? parameter="/images/myImage.jpeg">
那么在我們的JSP頁面,可以使用類似下面的例子:
??? <html:img action="downloadMyImage" alt="My Image" height="400" width="400"/>
??? <html:link action="downloadMyPdfFile">Click Here to See the PDF</html:link>
注意:我們可能要將struts配置文件中<controller>屬性的nocache值設(shè)置為false。如果設(shè)置為true,可能在IE上不能成功下載文件,但是在Firefox和Safari上工作正常。
??? <controller contentType="text/html;charset=UTF-8" locale="true" nocache="false" />
內(nèi)容部署(Content Disposition)
設(shè)置Content Disposition
DownloadAction不能處理content dispositon頭部。最簡單的方法是在getStreamInfo()方法中設(shè)置,比如:
public class ExampleFileDownload extends DownloadAction{
??? protected StreamInfo getStreamInfo(ActionMapping mapping,
?????????????????????????????????????? ActionForm form,
?????????????????????????????????????? HttpServletRequest request,
?????????????????????????????????????? HttpServletResponse response)
??????????? throws Exception {
??????? // File Name
??????? String fileName = mapping.getParameter();
??????? // Set the content disposition
??????? response.setHeader("Content-disposition",
?????????????????????????? "attachment; filename=" + new String(fileName.getBytes("gbk"), "iso8859_1"));// 設(shè)置文件名
??????? // Download a "pdf" file - gets the file name from the
??????? // Action Mapping's parameter
??????? String contentType = "application/pdf";
??????? File file????????? = new File(fileName);
??????? return new FileStreamInfo(contentType, file);
??? }
}
如果需要文件名做為參數(shù),可能需要首先把文件前面的任何路徑信息先清除。
@@ Content Disposition的值
我們可以設(shè)置content disposition來下載一個(gè)文件或者在瀏覽器中打開一個(gè)文件。
在瀏覽器中打開文件的例子寫法: "inline; filename=myFile.pdf"
下載的例子寫法: "attachment; filename=myFile.pdf"
顯示圖片的話,可以使用content disposition的"inline"選項(xiàng)。
文章來源:http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!784.entry