在Struts2中实现文件上传

//上传文件源代码

 

 

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.οnclick=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.οnclick=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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
校园短期闲置资源置换平台是一种创新的在线服务系统,旨在促进校园内学生、教职工以及其他成员之间的资源共享和有效利用。通过这个平台,用户可以发布、查找并交换他们暂时不使用的资源,从而实现资源的最大化利用,减少浪费,并促进校园内的可持续生活方式。以下是该平台可能包含的一些关键特性: 1. **用户注册和认证**:用户需要通过校园身份验证进行注册,确保平台的使用者都是校园社区的成员。 2. **资源发布**:用户可以发布他们愿意短期出借或交换的资源,如书籍、运动器材、电子产品、家具等。 3. **资源搜索和筛选**:用户可以根据资源类型、状态、可用时间等条件搜索和筛选所需的资源。 4. **在线预订系统**:用户可以通过平台预订所需的资源,并约定取用和归还的时间和地点。 5. **评价和信誉系统**:用户可以对交易的对方进行评价,建立信誉体系,增加用户间的信任。 6. **即时通讯功能**:平台内置即时通讯工具,方便用户就资源交换的细节进行沟通。 7. **安全和隐私保护**:确保用户的个人信息和交易记录的安全,保护用户的隐私。 8. **移动应用支持**:开发移动应用程序,使用户能够随时随地访问平台,进行资源的发布和搜索。 9. **教育资源置换**:特别为教师和研究人员提供教育资源的置换服务,如实验材料、研究工具等。 10. **活动和研讨会空间**:平台还可以用于发布和预订校园内临时活动或研讨会所需的空间。 11. **环保意识提升**:通过平台的推广和使用,增强校园社区成员的环保意识和资源节约意识。 12. **数据分析和报告**:平台可以收集和分析资源使用数据,为校园管理层提供资源利用和需求的洞察,以优化资源配置。 校园短期闲置资源置换平台通过提供一个便捷、高效和安全的在线环境,鼓励校园成员参与资源共享,不仅能够促进资源的合理分配和循环利用,还能增强社区的凝聚力和互助精神。随着共享经济理念的普及,这样的平台在校园的推广和应用将越来越广泛。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值