struts2 多文件上传

jsp上传页面代码:

<%@ page language="java" contentType="text/html; charset=gbk"

    pageEncoding="gbk"%>

<%@ 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>

<meta http-equiv="Content-Type" content="text/html; charset=gbk">

<title>Insert title here</title>

</head>

<script type="text/javascript">

    function clickme()

    {

        var divs=document.getElementById('mydiv');

        var br=document.createElement('br');

        var input=document.createElement('input');

        input.type="file";

        input.name="file"

        var button=document.createElement('input');

        button.type="button";

        button.value="remove";

        divs.appendChild(br);

        divs.appendChild(input);

        divs.appendChild(button);

        button.οnclick=function()

        {

            divs.removeChild(br);

            divs.removeChild(input);

            divs.removeChild(button);

        }

    }

</script>

<body>

<h1>Struts2</h1>

<s:fielderror name="file"/>

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

    <s:file name="file" theme="simple"></s:file>

    <input type="button" value="Add More" οnclick="clickme()"/>

    <div id="mydiv">

    </div>

    <s:textfield name="memo"></s:textfield>

    <s:submit></s:submit>

</s:form>

</body>

</html>


Actio 中代码 : 用户自己写代码来判断文件类型及文件大小

package com.struts2.action;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")

public class FileUploadAction extends ActionSupport {

         private List<File> file;

         private List<String> fileContentType;

         private List<String> fileFileName;

         private String memo;

         @Override

         public String execute() throws Exception {

                   String path=ServletActionContext.getRequest().getRealPath("/upload");

                   if(file==null)

                   {

                            this.addFieldError("file", "文件不能为空,请选择");

                            return INPUT;

                   }else

                   {

                            InputStream is = null;

                            OutputStream os = null;

                            File f=null;

                            long fileSize=0;

                            String ext="";

                            for (int i = 0; i < file.size(); i++) {

                                     ext=fileFileName.get(i).substring(fileFileName.get(i).lastIndexOf(".")+1);

                                     f=this.getFile().get(i);

                                     fileSize=f.length();

                                     System.out.println("fileSize:"+fileSize);

                                     System.out.println("ext:"+ext);

                                     if("exe".equals(ext)||"jar".equals(ext)||"bat".equals(ext)||"msi".equals(ext))

                                     {

                                               this.addFieldError("file", "the file is not allowed");

                                               return INPUT;

                                     }

                                    

                                     if(fileSize>102000)

                                     {

                                               this.addFieldError("file", "the file is too large");

                                               return INPUT;

                                     }

                                    

                                     is=new FileInputStream(f);

                                     os=new FileOutputStream(new File(path,this.getFileFileName().get(i)));

                                     byte[] buf=new byte[1024];

                                     int length=0;

                                     while((length=is.read(buf))>0)

                                     {

                                               os.write(buf, 0, length);

                                      }

                            }

                            is.close();

                            os.close();

                   }

                   return SUCCESS;

         }

         public List<File> getFile() {

                   return file;

         }

         public void setFile(List<File> file) {

                   this.file = file;

         }

         public List<String> getFileContentType() {

                   return fileContentType;

         }

         public void setFileContentType(List<String> fileContentType) {

                   this.fileContentType = fileContentType;

         }

         public List<String> getFileFileName() {

                   return fileFileName;

         }

         public void setFileFileName(List<String> fileFileName) {

                   this.fileFileName = fileFileName;

         }

         public String getMemo() {

                   return memo;

         }

         public void setMemo(String memo) {

                   this.memo = memo;

         }

}

Struts.xml 中配置代码 :

<!-- 文件上传Action -->

               <action name="Upload" class=" com.struts2.action.FileUploadAction">
            <result name="success">/success.jsp</result>

            <result name="input">/upload.jsp</result>

            <interceptor-ref name="fileUpload">

            <!--

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

               <param name="allowedTypes">application/msword</param>

             -->

            </interceptor-ref>

            <interceptor-ref name="defaultStack"/>   

        </action>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值