Struts 2 - 文件上传

Struts 2框架提供了使用"基于表单的HTML文件上传"处理文件上载的内置支持.上传文件时,它通常会存储在临时目录中,并且应由Action类处理或移动它们到永久目录,以确保数据不会丢失.

注意 : 服务器可能有一个安全策略,禁止您写入临时目录以外的目录以及属于您的Web应用程序的目录.

Struts中的文件上载可以通过pre -defined拦截器名为 FileUpload 拦截器,可通过org.apache.struts2.interceptor.FileUploadInterceptor类获得,并作为 defaultStack 的一部分包含在内.您仍然可以在struts.xml中使用它来设置各种参数,如下所示.

创建视图文件

让我们从创建开始浏览和上传所选文件所需的视图.因此,让我们使用纯HTML上传表单创建 index.jsp ,允许用户上传文件 :

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"   pageEncoding = "ISO-8859-1"%><%@ taglib prefix = "s" uri = "/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd"><html>   <head>      <title>File Upload</title>   </head>      <body>      <form action = "upload" method = "post" enctype = "multipart/form-data">         <label for = "myFile">Upload your file</label>         <input type = "file" name = "myFile" />         <input type = "submit" value = "Upload"/>      </form>   </body></html>

上面的例子中有几点值得注意.首先,表单的enctype设置为 multipart/form-data .应设置此选项,以便文件上载拦截器成功处理文件上载.下一点需要注意的是表单的操作方法上传以及文件上传字段的名称 -   myFile .我们需要这些信息来创建action方法和struts配置.

接下来,让我们创建一个简单的jsp文件 success.jsp 来显示我们的结果文件上传,以防它成功.

<%@ page contentType = "text/html; charset = UTF-8" %><%@ taglib prefix = "s" uri = "/struts-tags" %><html>   <head>      <title>File Upload Success</title>   </head>      <body>      You have successfully uploaded <s:property value = "myFileFileName"/>   </body></html>

以下是结果文件 error.jsp ,以防上传文件时出现错误 :

<%@ page contentType = "text/html; charset = UTF-8" %><%@ taglib prefix = "s" uri = "/struts-tags" %><html>   <head>      <title>File Upload Error</title>   </head>      <body>      There has been an error in uploading the file.   </body></html>

创建操作类

接下来,让我们创建一个名为 uploadFile.java 将负责上传文件并将该文件存储在安全位置 :

package com.it1352.struts2; import java.io.File;import org.apache.commons.io.FileUtils;import java.io.IOException; import com.opensymphony.xwork2.ActionSupport;public class uploadFile extends ActionSupport {   private File myFile;   private String myFileContentType;   private String myFileFileName;   private String destPath;   public String execute() {      /* Copy file to a safe location */      destPath = "C:/apache-tomcat-6.0.33/work/";      try {         System.out.println("Src File name: " + myFile);         System.out.println("Dst File name: " + myFileFileName);                   File destFile  = new File(destPath, myFileFileName);         FileUtils.copyFile(myFile, destFile);        } catch(IOException e) {         e.printStackTrace();         return ERROR;      }      return SUCCESS;   }      public File getMyFile() {      return myFile;   }      public void setMyFile(File myFile) {      this.myFile = myFile;   }      public String getMyFileContentType() {      return myFileContentType;   }      public void setMyFileContentType(String myFileContentType) {      this.myFileContentType = myFileContentType;   }      public String getMyFileFileName() {      return myFileFileName;   }      public void setMyFileFileName(String myFileFileName) {      this.myFileFileName = myFileFileName;   }}

uploadFile.java 是一个非常简单的类.需要注意的重要一点是,FileUpload拦截器和参数拦截器为我们完成了所有繁重工作.

FileUpload拦截器默认为您提供三个参数.它们以下列模式命名 :

  • [您的文件名参数] : 这是用户上传的实际文件.在此示例中,它将是"myFile"

  • [您的文件名参数] ContentType : 这是上载文件的内容类型.在此示例中,它将是"myFileContentType"

  • [您的文件名参数] FileName : 这是上载的文件的名称.在这个例子中,它将是"myFileFileName"

由于Struts拦截器,我们可以使用这三个参数.我们所要做的就是在Action类中使用正确的名称创建三个参数,并自动为我们自动连接这些变量.所以,在上面的例子中,我们有三个参数和一个动作方法,只要一切正常就返回"成功",否则返回"错误".

配置文件

以下是控制文件上传过程的Struts2配置属性 :

Sr.NoProperties&说明
1

struts.multipart.maxSize

要接受为文件上载的文件的最大大小(以字节为单位).默认值为250M.

2

struts.multipart.parser

用于上传多部分表单的库.默认为 jakarta

3

struts.multipart.saveDir

存储临时文件的位置.默认情况下是javax.servlet.context.tempdir.

为了改变任何这些设置,您都可以在应用程序struts.xml文件中使用常量标记,就像我更改要上载的文件的最大大小一样.

让我们将 struts.xml 设置如下 :

<?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.multipart.maxSize" value = "1000000" />   <package name = "helloworld" extends = "struts-default">      <action name = "upload" class = "com.IT屋.struts2.uploadFile">         <result name = "success">/success.jsp</result>         <result name = "error">/error.jsp</result>      </action>   </package></struts>

因为 FileUpload 拦截器是默认拦截器堆栈的一部分,所以我们不需要明确配置它.但是,你可以添加< interceptor-ref>标签内< action>. fileUpload拦截器接受两个参数(a)maximumSize (b)allowedTypes .

maximumSize参数设置允许的最大文件大小(默认值约为2MB). allowedTypes参数是以逗号分隔的接受内容(MIME)类型列表,如下所示 :

<action name = "upload" class = "com.IT屋.struts2.uploadFile">   <interceptor-ref name = "basicStack">   <interceptor-ref name = "fileUpload">      <param name = "allowedTypes">image/jpeg,image/gif</param>   </interceptor-ref>   <result name = "success">/success.jsp</result>   <result name = "error">/error.jsp</result></action>

以下是 web.xml 文件的内容 :

<?xml version = "1.0" Encoding = "UTF-8"?><web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   xmlns = "http://java.sun.com/xml/ns/javaee"    xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   id = "WebApp_ID" version = "3.0">      <display-name>Struts 2</display-name>      <welcome-file-list>      <welcome-file>index.jsp</welcome-file>   </welcome-file-list>      <filter>      <filter-name>struts2</filter-name>      <filter-class>         org.apache.struts2.dispatcher.FilterDispatcher      </filter-class>   </filter>   <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</url-pattern>   </filter-mapping></web-app>

现在右键单击项目名称,然后单击导出> WAR文件以创建War文件.然后在Tomcat的webapps目录中部署此WAR.最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/upload.jsp .这将产生以下屏幕 :

Hello World Struts 7

现在使用"浏览"按钮选择文件"Contacts.txt",然后单击"上传"按钮,该按钮将在您的服务上传文件,您应该会看到下一页.您可以检查上传的文件是否应该保存在C:\ apache-tomcat-6.0.33 \ work.

Hello World Struts 8

请注意,FileUpload Interceptor会自动删除上传的文件,因此您必须在删除之前在某个位置以编程方式保存上传的文件.

错误消息

fileUplaod拦截器使用几个默认错误消息键 :

Sr.No错误消息密钥&说明
1

struts.messages.error.uploading

无法上传文件时发生的一般错误.

2

struts.messages.error.file. too.large

当上传的文件太大而且由maximumSize指定时发生.

3

struts.messages.error.content.type.not.允许

当上传的文件与指定的预期内容类型不匹配时发生.

您可以在 WebContent/WEB-INF/classes/messages.properties 资源文件中覆盖这些消息的文本.

免责声明:以上内容(如有图片或视频亦包括在内)有转载其他网站资源,如有侵权请联系删除

咨询热线(9:00 - 18:00)
0755 - 29812418
微信公众号二维码
微信公众号二维码
微信公众号
返回顶部