在Struts2中实现文件上传

来源:互联网 发布:淘宝店铺关联打印 编辑:程序博客网 时间:2024/05/18 00:57

//上传文件源代码

 

 

package com.shunwang.actions;

 

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;

 

 

public class UploadAction extends ActionSupport {

 

    private String username;

 

    private String password;

 

    private List<File> file;

 

    private List<String> fileFileName;  //必须含有的字段

 

    private List<String> fileFileContentType; //必须含有的字段

 

    public String getUsername() {

       return username;

    }

 

    public void setUsername(String username) {

       this.username = username;

    }

 

    public String getPassword() {

       return password;

    }

 

    public void setPassword(String password) {

       this.password = password;

    }

 

    public List<File> getFile() {

       return file;

    }

 

    public void setFile(List<File> file) {

       this.file = file;

    }

 

    public List<String> getFileFileName() {

       return fileFileName;

    }

 

    public void setFileFileName(List<String> fileFileName) {

       this.fileFileName = fileFileName;

    }

 

    public List<String> getFileFileContentType() {

       return fileFileContentType;

    }

 

    public void setFileFileContentType(List<String> fileFileContentType) {

       this.fileFileContentType = fileFileContentType;

    }

 

    @Override

    public String execute() throws Exception {

 

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

       {

           InputStream is=new FileInputStream(file.get(i));

           String root = ServletActionContext.getServletContext().getRealPath("//");

           File file=new File(root,fileFileName.get(i));

           OutputStream os=new FileOutputStream(file);

           byte buffer[]=new byte[1024];

           while(is.read(buffer)>0)

           {

              os.write(buffer);

           }

           os.close();

           is.close();

       }

           return SUCCESS;

 

    }

 

}

 

Struts.xml的配置文件

 

 

<action name="upload" class="com.shunwang.actions.UploadAction">

           <result>/uploadSuccess.jsp</result>

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

           <interceptor-ref name="fileUpload">

              <param name="maximumSize">1048576</param>  <!-- 设置上传的最大值 本例为1MB 1024bit*1024 -->

              <param name="allowedTypes">image/jpeg</param><!-- 设置允许的上传文件类型 -->

           </interceptor-ref>

           <interceptor-ref name="defaultStack"></interceptor-ref>

       </action>

 

 

//动态加载上传文件数量的JavaScript代码段

 

<script type="text/javascript">

function addMore()

{

    var td=document.getElementById("td");

   

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

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

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

   

    input.type="file";

    input.name="file";

   

    button.type="button";

    button.value="移除该文件"

   

    button.onclick=function()

    {

       if(confirm("确定删除对该文件的上传?"))

       {

           td.removeChild(br);

           td.removeChild(input);

           td.removeChild(button);

       }

    }

   

    td.appendChild(br);

    td.appendChild(input);

    td.appendChild(button);

   

}

//示例jsp页面

 

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

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

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

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

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'upload.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

<script type="text/javascript">

function addMore()

{

    var td=document.getElementById("td");

   

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

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

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

   

    input.type="file";

    input.name="file";

   

    button.type="button";

    button.value="移除该文件"

   

    button.onclick=function()

    {

       if(confirm("确定删除对该文件的上传?"))

       {

           td.removeChild(br);

           td.removeChild(input);

           td.removeChild(button);

       }

    }

   

    td.appendChild(br);

    td.appendChild(input);

    td.appendChild(button);

   

}

</script>

  </head>

 

  <body>

 

<table width="25%" align="center" border="1"><tr><td align="center">

<strong><font color="red">

<s:fielderror/></font></strong></td></tr></table>

<s:form action="upload" method="post" enctype="multipart/form-data" theme="simple">

 <table width="25%" border="1" align="center">

<tr><td>username:</td><td>

<s:textfield name="username" label="username"></s:textfield></td></tr><tr><td>password:</td><td>

<s:password name="password" label="password"></s:password></td></tr><tr><td>file:</td><td id="td">

<s:file name="file" label="file"></s:file><input type="button" onclick="addMore()" value="添加更多文件上传"></td>

</tr>

<tr>

<td colspan="2" align="right">

<s:submit value="  提交  "></s:submit>&nbsp;&nbsp;</td>

</tr>

</table>

 

</s:form>

 

  </body>

</html>

原创粉丝点击