Struts2上传

1). 表单需要注意的 1 点
  enctype="multipart/form-data"

<body>

    <s:debug></s:debug>
    
    <s:form action="testUpload" 
        method="post" enctype="multipart/form-data">
        
         <s:file name="ppt" label="PPTFile"></s:file>
        <s:textfield name="pptDesc" label="PPTDesc"></s:textfield>
        
        <s:submit></s:submit>
    </s:form>
    
</body>


2). Struts2 的文件上传实际上使用的是 Commons FileUpload 组件, 所以需要导入

commons-fileupload-1.3.jar
commons-io-2.0.1.jar

3). Struts2 进行文件上传需要使用 FileUpload 拦截器

4). 基本的文件的上传: 直接在 Action 中定义如下 3 个属性, 并提供对应的 getter 和 setter

//文件对应的 File 对象
private File [fileFieldName];
//文件类型
private String [fileFieldName]ContentType;
//文件名
private String [fileFieldName]FileName;

5). 使用 IO 流进行文件的上传即可.

public String execute()  {
        
        System.out.println(ppt);
        System.out.println(pptContentType);
        System.out.println(pptFileName);
        System.out.println(pptDesc);
    //用ServletContext得到上传文件到达的绝对路径 ServletContext servletContext
=ServletActionContext.getServletContext(); String dir = servletContext.getRealPath("/files/"+pptFileName); System.out.println(dir); try {
      //得到的路径输出。 out
=new FileOutputStream(dir); } catch (FileNotFoundException e) { e.printStackTrace(); } try {
      //写入到Tomcat项目下。 in
= new FileInputStream (ppt); } catch (FileNotFoundException e) { e.printStackTrace(); } //设置字节的大小 byte[] buffer=new byte[1024]; int len=0; try {
    //读到的文件写入。
while((len=in.read(buffer))!= -1){ out.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } return "success"; }

*:多个文件上传。用List集合。但是回显的时候。会显示整个集合里的值。用下标的方式

<body>

    <s:debug></s:debug>
    
    <s:form action="testUpload" 
        method="post" enctype="multipart/form-data">
        
         <s:file name="ppt" label="PPTFile"></s:file>
        <s:textfield name="pptDesc[0]" label="PPTDesc"></s:textfield>
        
         <s:file name="ppt" label="PPTFile"></s:file>
        <s:textfield name="pptDesc[1]" label="PPTDesc"></s:textfield>
        
         <s:file name="ppt" label="PPTFile"></s:file>
        <s:textfield name="pptDesc[2]" label="PPTDesc"></s:textfield>
        <s:submit></s:submit>
    </s:form>
    
</body>
   private List<File> ppt;
    private List<String> pptContentType;
    private List<String> pptFileName;
    private List<String> pptDesc;

**:限制上传文件。然后是由于什么原因上传不了。显示出来

可以通过配置 FileUploadInterceptor 拦截器的参数的方式来进行限制

maximumSize (optional) - 默认的最大值为 2M. 上传的单个文件的最大值

allowedTypes (optional) - 允许的上传文件的类型. 多个使用 , 分割

allowedExtensions (optional) - 允许的上传文件的扩展名. 多个使用 , 分割.

<interceptors>
            <interceptor-stack name="zidingyilanjieqi">
                <interceptor-ref name="defaultStack">
                    <param name="fileUpload.maximumSize">2097152</param>
                    <param name="fileUpload.allowedTypes">text/html,text/xml</param>
                    <param name="fileUpload.allowedExtensions">html,dtd,xml</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>
    
    <!-- 使用默认拦截器 -->
    <default-interceptor-ref name="zidingyilanjieqi"></default-interceptor-ref>


注意: 在 org.apache.struts2 下的 default.properties 中有对上传的文件总的大小的限制. 可以使用常量的方式来修改该限制

struts.multipart.maxSize=2097152

<constant name="struts.multipart.maxSize" value="1045732" />

定制错误消息. 可以在国际化资源文件中定义如下的消息:

在struts.xml中使用国际化要配置:

<!-- 配置国际化资源文件 -->
    <constant name="struts.custom.i18n.resources" value="i18n"></constant>

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, 可以提供更多的定制信息.

通过配置FileUploadInterceptor拦截器的参数的方式来进行限制

struts.messages.error.uploading=^Error uploading: {0}
struts.messages.error.file.too.large=^The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=^Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=^File extension not allowed: {0} "{1}" "{2}" {3}

struts.messages.invalid.token=^^The form has already been processed or no token was supplied, please try again.

***:Struts2常量的具体用法实例

 <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 --> 

<constant name="struts.i18n.encoding" value="UTF-8" /> 

 ****:自己整理思路:

1:得到页面上文件的属性。
//文件对应的 File 对象
private File [fileFieldName];
//文件类型
private String [fileFieldName]ContentType;
//文件名
private String [fileFieldName]FileName;
2:放到创建的文件夹中
    1):先得到文件夹的绝对路径
    ServletContext servletContext=ServletActionContext.getServletContext();
        String dir = servletContext.getRealPath("/files/"+pptFileName);
        System.out.println(dir);
    2):利用IO流把上传的文件写到创建的文件夹中
    try {
            out =new FileOutputStream(dir);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            in = new FileInputStream (ppt);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
        byte[] buffer=new byte[1024];
        int len=0;
        try {
            while((len=in.read(buffer))!= -1){
                out.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    3:利用FileUploadInterceptor拦截器设置一些上传的信息
<interceptors>
            <interceptor-stack name="zidingyilanjieqi">
                <interceptor-ref name="defaultStack">
                    <param name="fileUpload.maximumSize">2097152</param>
                    <param name="fileUpload.allowedTypes">text/html,text/xml</param>
                    <param name="fileUpload.allowedExtensions">html,dtd,xml</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>
    
    <!-- 使用默认拦截器 -->
    <default-interceptor-ref name="zidingyilanjieqi"></default-interceptor-ref>
4:利用国际化资源文件设置一些错误信息。
struts.messages.error.uploading=^Error uploading: {0}
struts.messages.error.file.too.large=^The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=^Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=^File extension not allowed: {0} "{1}" "{2}" {3}
struts.messages.invalid.token=^^The form has already been processed or no token was supplied, please try again.
原文地址:https://www.cnblogs.com/bulrush/p/7768574.html