`
esffor
  • 浏览: 1362860 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

《struts2权威指南》学习笔记之struts2多文件上传--使用数组方式

阅读更多

上传页面:

 

<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%...@taglib prefix="s" uri="/struts-tags"%>
<%...@ page isELIgnored="false" %>
<%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<head>

<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
<s:fielderror/>
<form action="upload.action" method="post" enctype="multipart/form-data">
      
<input type="text" name="title" /><br>
      
<input type="file" name="upload" /><br>
      
<input type="file" name="upload" /><br>
      
<input type="file" name="upload" /><br>
    
<input value="upload" type="submit" />
</form>
</body>
</html>

 上传Action

 

package lee;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import org.apache.struts2.ServletActionContext;
import java.io.*;

import com.opensymphony.xwork2.ActionSupport;

/** *//**
 * 
@author  yeeku.H.lee kongyeeku@163.com
 * 
@version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 
*/


public class UploadAction extends ActionSupport
...{
    
private String title;
    
private File[] upload;
    
private String[] uploadContentType;
    
private String[] uploadFileName;


    
//接受依赖注入的属性
    private String savePath;
    
//接受依赖注入的方法
    public void setSavePath(String value)
    
...{
        
this.savePath = value;
    }


    
private String getSavePath() throws Exception 
    
...{
        
return ServletActionContext.getRequest().getRealPath(savePath);
    }

    
    
public void setTitle(String title) ...{
        
this.title = title; 
    }


    
    
public File[] getUpload() ...{
        
return upload;
    }


    
public void setUpload(File[] upload) ...{
        
this.upload = upload;
    }


    
public String[] getUploadContentType() ...{
        
return uploadContentType;
    }


    
public void setUploadContentType(String[] uploadContentType) ...{
        
this.uploadContentType = uploadContentType;
    }


    
public String[] getUploadFileName() ...{
        
return uploadFileName;
    }


    
public void setUploadFileName(String[] uploadFileName) ...{
        
this.uploadFileName = uploadFileName;
    }


    
public String getTitle() ...{
        
return title;
    }


    @Override
    
public String execute() throws Exception
    
...{
        System.out.println(
"开始上传单个文件-----------------------");
        System.out.println(getSavePath());
        System.out.println(
"==========" + getUploadFileName());
        System.out.println(
"==========" + getUploadContentType());
        System.out.println(
"==========" + getUpload());
        
        File[] files
=this.getUpload();
        
for(int i=0;i<files.length;i++)...{
            
//以服务器的文件保存地址和原文件名建立上传文件输出流
            FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getUploadFileName()[i]);
            FileInputStream fis 
= new FileInputStream(files[i]);
            
byte[] buffer = new byte[1024];
            
int len = 0;
            
while ((len = fis.read(buffer)) > 0)
            
...{
                fos.write(buffer , 
0 , len);
            }

        }

        
return SUCCESS;
    }

    



}

 

struts.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
    
<struts>

    
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
    
<constant name="struts.i18n.encoding" value="GBK"/>

    
<package name="lee" extends="struts-default">
    
        
<action name="upload" class="lee.UploadAction">
          
<interceptor-ref name="fileUpload"> 
                
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> 
                
<param name="maximumSize">2000</param> 
            
</interceptor-ref> 
            
<interceptor-ref name="defaultStack"/> 
            
<param name="savePath">/upload</param>
            
<result>/succ.jsp</result>    
            
<result name="input">/upload.jsp</result>    
        
</action>        
    
</package>
</struts>    

 

结果显示页面:

 

<%
分享到:
评论

相关推荐

    Struts2权威指南完整版

    改为使用Convention插件提供“零配置”,Struts 2.1新增了Portlet支持……为了让众多Struts学习者、工作者快速从Struts 2.0的开发升级到Struts 2.1,笔者升级了《Struts 2权威指南》,第二版改写了第一版中所有程序...

    Struts2权威指南-带目录索引完整版.pdf

    Struts2权威指南 带目录索引完整版

    [Struts 2权威指南--基于WebWork核心的MVC开发(高清完整版) 1/12

    不用多说了,Struts 2权威指南--基于WebWork核心的MVC开发(高清完整版),解压出来有200多M,因为权限不怎么够,我一共分了12卷,是一本不可多得的好书。第一卷附目录: 第1章 Struts 2概述,第2章 Struts 2下的Hello...

    Struts2权威指南 加源码

    5. **国际化**:通过资源包(properties文件),Struts2支持多语言环境,方便不同地区用户的使用。 6. **安全考虑**:Struts2的安全漏洞曾引起广泛关注,理解并防范XSS、CSRF等攻击是每个开发者必备的知识。 总的来...

    struts2权威指南源代码

    7. **国际化与本地化**:Struts2支持多语言环境,源代码中可能会包含如何配置资源文件,以及如何在视图中使用`s:text`标签实现国际化。 8. **异常处理**:Struts2提供了全局和局部的异常处理机制,通过`&lt;global-...

    Struts2权威指南.pdf 清晰中文完整版

    这本书《Struts2权威指南》显然是针对想要学习或深入理解Struts2框架的开发者设计的。下面我们将详细探讨Struts2的一些核心知识点。 1. **Struts2框架概述**:Struts2是Apache软件基金会的开源项目,它是Struts1的...

    struts2四天的学习笔记

    13. ** strut2四天笔记**:这份学习笔记可能涵盖了以上所有知识点,包括如何创建Action,配置struts.xml,使用OGNL表达式,处理异常,以及实践中的各种技巧和最佳实践。 在四天的学习过程中,你应该通过实践和理解...

    struts2 学习重点笔记

    ### Struts2 学习重点知识点总结 #### 一、Struts2 概念与架构 **1.1 Struts2 简介** - **定义**:Struts2 是 Apache 组织提供的一个基于 MVC 架构模式的开源 Web 应用框架。 - **核心**:Struts2 的核心其实是 ...

    struts2 权威指南完整版PDF

    Struts2是Apache软件基金会开发的...以上只是Struts2框架的部分知识点,实际的《Struts2权威指南》会更加详尽地探讨这些主题,并涵盖更多高级特性和实践案例。学习和掌握Struts2,将有助于提升Java Web开发的专业技能。

    struts2学习笔记黑马程序员

    ### Struts2学习笔记之文件上传与Ajax开发 #### Struts2文件上传 **文件上传简介** 文件上传是Web应用中常见的功能之一,Struts2框架内置了对文件上传的支持,使得开发者能够轻松地实现这一功能。为了确保文件...

    Struts 2权威指南源码.rar

    这个"Struts 2权威指南源码.rar"压缩包文件包含了《Struts 2权威指南》一书中的示例代码,这些源码对于学习和理解Struts 2框架的工作原理非常有帮助。 在深入探讨源码之前,让我们先了解Struts 2的核心特性: 1. *...

    struts2权威指南 电子书 配套源码-1

    struts2权威指南 电子书 配套源码 文件大小153mb 分为8个压缩包,每分卷1分,每分卷20兆(最多只让上传20兆,没有办法) struts2权威指南电子书连接:http://sdan250.download.csdn.net/user/sdan250/all/3

    struts2权威指南源码

    Struts2在后续的版本中修复了许多安全漏洞,引入了更多的新特性和改进,例如从WebWork过渡到完全的Struts2命名,以及安全性增强等。 6. **源码学习价值**: 分析Struts2的源码可以帮助我们理解MVC框架的设计思想,...

    Struts2权威指南完整版PDF

    Struts2权威指南完整版PDFStruts2权威指南完整版PDF

    Struts2权威指南全部源码

    总的来说,"Struts2权威指南全部源码"是一份宝贵的资源,通过深入学习和实践,你可以提升对Struts2的理解,掌握Web应用开发的关键技能。但同时,也要准备好面对可能的挑战,因为学习过程中遇到错误是正常的,关键...

    Struts2权威指南完整版.pdf

    Struts2权威指南完整版.pdf 不错的Struts入门教程 也可以当工具书

    Struts2.1权威指南——基于WebWork核心的MVC开发.zip

    《Struts 2.1权威指南》特点为:1.经验丰富,针对性强 《Struts 2.1权威指南》凝聚了作者大量的实际开发经验和感悟。作者依照读者的学习规律,首先介绍基本概念和基本操作,然后对内容进行深入讲解。 2.讲解具体,...

    Struts2权威指南(完整版)含源码part13

    Struts2权威指南(完整版)含源码 Struts2权威指南(完整版)含源码

    Struts2权威指南(全).pdf

    在“Struts2权威指南(全).pdf”这本书中,读者可以全面深入地学习Struts2的核心概念、架构、最佳实践以及高级特性。 1. **Struts2基础**:Struts2的核心是Action类,它是处理用户请求并执行业务逻辑的主要组件。每...

Global site tag (gtag.js) - Google Analytics