Struts 2中使用的文件上传拦截器添加在Struts应用程序上传文件的支持服务。 基于Struts 2的文件上传拦截MultiPartRequestWrapper,这是自动应用的要求,如果它包含文件中的元素。然后,它增加了以下参数要求(假设上传的文件名 是文件MyFile)。
- MyFile :文件-实际的文件
- MyFileContentType :字符串-该文件的内容类型
- MyFileFileName:字符串-上传文件的实际名称(而不是HTML的名称)
在Action类中,你可以得到刚刚创建getter和setter方法的文件,上传文件的名称和内容类型。例如,setMyfile(档案文件),setMyFileContentType(字符串的contentType),getMyFile()等。
文件上传拦截器也没有验证,并增加了错误,这些错误信息存储在struts-messsages.properties文件。消息的值可以是通过提供以下键文本重写:
- struts.messages.error.uploading - 错误上传文件时失败
- struts.messages.error.file.too.large - 发生错误时,文件的大小是大
- struts.messages.error.content.type.not.allowed - 当内容类型是不允许的
Parameters参数
你可以使用下列参数来控制文件上传功能。
- maximumSize 此参数是可选的。 这个默认值是2MB。
- allowedTypes 这个参数也是可选的。 它允许你指定允许的内容类型。
编写上传文件的示例代码
现在我们将编写代码上传服务器上的文件。
Action Class
在Action类中,我们将定义四个属性,以方便文件上传。
privateFile upload ; // The actual file
privateString uploadContentType ;// The content type of the file
privateString uploadFileName ;// The uploaded file name and path
privateString fileCaption ;// The caption of the file entered by user.
这里是完整的代码的action class StrutsFileUpload.java
packagenet.roseindia; |
在这里,我们没有保存上传文件的代码。 但它可以使用在执行下面的代码(..)动作类的方法,很容易做到。 Here is code snippet. 下面是代码片段。
/ /下面的代码可以用来保存上传文件
try{
String fullFileName ="c:/upload/myfile.txt" ;
File theFile =new File(fullFileName);
FileUtils.copyFile(upload , theFile);
}catch (Exception e) {
addActionError(e.getMessage());
return INPUT;
} }
Writing JSP page编写JSP页面
这里是jsp文件(upload.jsp)的代码,显示上传文件形式提供给用户。
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>File Upload Example</title> <link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/> </head> <body> <s:actionerror /> <s:fielderror /> <s:form action="doUpload" method="POST" enctype="multipart/form-data "> <tr> <td colspan="2"><h1>File Upload Example</h1></td> </tr> <s:file name="upload" label="File"/> <s:textfield name="caption" label="Caption"/> <s:submit /> </s:form> </body> </html> |
在上面的代码形式的加密类型的“multipart / form-data的 ”和<s:file ../>标签呈现HTML文件标记。
File upload success page文件上传成功页面
下面是文件上传(上传的success.jsp)成功页面的代码。
<%@ page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Showcase</title> <link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/> </head> <body> <table class="wwFormTable"> <tr> <td colspan="2"><h1>File Upload Example</h1></td> </tr> <tr> <td class="tdLabel"><label for="doUpload_upload" class="label">Content Type:</label></td> <td> <s:property value="uploadContentType" /> </td> </tr> <tr> <td class="tdLabel"><label for="doUpload_upload" class="label">File Name:</label></td> <td > <s:property value="uploadFileName" /> </td> </tr> <tr> <td class="tdLabel"><label for="doUpload_upload" class="label">File:</label></td> <td> <s:property value="upload" /> </td> </tr> <tr> <td class="tdLabel"><label for="doUpload_upload" class="label">File Caption:</label></td> <td> <s:property value="caption" /> </td> </tr> </table> </body> </html> |
在struts.xml文件中添加映射
在struts.xml文件中加入下面的映射。
<!-- File Upload -->
<action name="showUpload">
<result>/pages/upload.jsp</result>
</action>
<action name="doUpload" class="net.roseindia.StrutsFileUpload">
<result name="input">/pages/upload.jsp</result>
<result>/pages/upload-success.jsp</result>
</action>
<!-- End File Upload -->
“showUpload”行动显示上传的形式和的“doUpload”行动实际上上传的文件。
运行示例
要测试应用程序的编译代码,然后运行Tomcat。类型http://localhost:8080/struts2tutorial/roseindia/showUpload.action在您的浏览器。您的浏览器应该显示下列形式:
现在浏览的文件,输入标题,然后点击“提交”按钮。应用程序将上传您的文件,然后屏幕将显示以下成功。
有一个很重要的一点有关文件上传拦截器时,要注意。文件上传拦截器实际上删除的上传,一旦行动被执行。下面是Tomcat的屏幕截图显示的文件删除消息:
INFO: Removing file upload C:\apache-tomcat-6.0.10Struts2\apache-tomcat-6.0.10\work\Catalina\localhost\struts2tutorial\upload__13f532f7_1132e1d4754__8000_00000000.tmp
在本节中,你了解到在Struts 2的文件上传概念。
原文出处:http://www.roseindia.net/struts/struts2/struts-2-file-upload.shtml;