单文件上传

1.要使用html的表单上传文件,必须把html表单的enctype属性设置为multipart/form-data,

method必须设置为post

2.使用File Upload拦截器来进行上传文件的管理.这个拦截器有两个属性:

maximumSize---文件上传的最大长度(单位是字节),默认值为2MB.

allowedTypes---文件上传的类型限制,用逗号分隔.

 

e.g:

 

SingleUpload.jsp内容

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>SingleUpload</title>

  </head>

 

  <body>

    <h1>单文件上传</h1>

    <s:fielderror />

    <s:form action="singleUpload" enctype="multipart/form-data">

        <s:file name="upload" label="选择文件" />

<s:submit value="提交" />

    </s:form>

  </body>

</html>

 

 

Struts.xml文件内容

<package name="default" namespace="/" extends="struts-default">

    <default-action-ref name="index" />

    <action name="singleUpload" class="com.upload.action.SingleFileUpload" method="uploader">

       <interceptor-ref name="fileUpload">

           <!-- 限定大小 -->

           <param name="maximumSize">100000</param>

           <!-- 限定格式 -->

<param

name="allowedTypes">image/gif,image/jpeg,image/png,text/plain</param>

       </interceptor-ref>

<!--使用struts2自带的拦截器,basicStack切记要写,否则文件不会上传成功 -->

       <interceptor-ref name="basicStack" />

        <result>/SingleUpload.jsp</result>

       <result name="error">/error.jsp</result>

    </action>

</package>

 

 

 

SingleFileUpload.java内容

package com.upload.action;

 

import java.io.File;

 

import javax.servlet.ServletContext;

 

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class SingleFileUpload extends ActionSupport {

   

    private File upload;

    private String uploadFileName;

    private String uploadContentType;

 

    /*省略gettersetter*/

 

    public String uploader(){

       System.out.println(uploadFileName);

       ServletContext context=ServletActionContext.getServletContext();

       if(upload!=null){

           String dataDir = context.getRealPath("/uploads");

           File savedFile = new File(dataDir,uploadFileName);

           upload.renameTo(savedFile);

       }

       return SUCCESS;

    }

}

 

 

 

多文件上传

MultipleFileUpload.jsp内容

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>MultipleFileUpload</title>

  </head>

 

  <body>

  <s:form action="multiple/multipleUpload" enctype="multipart-data" method="post">

    <s:file name="upload" lable="文件1"/>

    <s:file name="upload" lable="文件2"/>

    <s:file name="upload" lable="文件3"/>

    <s:submit value="提交" />

  </s:form>

 

  </body>

</html>

 

 

 

Struts.xml内容

<action name="multipleUpload" class="com.upload.action.MultipleFileUpload" method="uploader">

    <interceptor-ref name="fileUpload">

       <param name="maximumSize">102400</param>

<param

name="allowedTypes">image/gif,image/jpeg,image/png,text/plain

</param>

    </interceptor-ref>

    <interceptor-ref name="basicStack" />

    <result>/index.jsp</result>

    <result name="error">/index.jsp</result>

</action>

 

 

 

 

MultipleFileUpload.java内容

 

package com.upload.action;

 

import java.io.File;

 

import javax.servlet.ServletContext;

 

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class MultipleFileUpload extends ActionSupport {

 

    private File[] upload;

    private String[] uploadFileName;

    private String[] uploadContentType;

 

    /*省略gettersetter*/

   

 

    public String uploader() {

       System.out.println(upload);

       ServletContext context = ServletActionContext.getServletContext();

       if (upload!=null) {

           String dataDir = context.getRealPath("/uploads");

           for(int i=0;i<upload.length;i++){

              File savedFile = new File(dataDir, uploadFileName[i]);

              upload[i].renameTo(savedFile);

           }

       }

       return SUCCESS;

    }

}

 

还可以用List来实现多文件的上传,不过用数组比用List,因为可以通过索引来对文件进行遍历.