第01步:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

第02步:编写action类

package com.self.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; /**文件上传**/
public class FileUpload {
private File[] image;//得到上传的文件
private String[] imageFileName;//获取上传文件的名称,命名规则:页面属性名+FileName
private String[] imageContentType;//得到上传文件的类型 public FileUpload() {
} /**上传方法**/
public String uploadFile(){
try {
String realPath=ServletActionContext.getServletContext().getRealPath("/images");
System.out.println("工程路径(/images路径,文件保存路径):"+realPath);
if(image!=null){
System.out.println("已经获得上传文件!");
File parentFile=new File(realPath);//指定文件保存路径
if(!parentFile.exists()){//保存路径不存在,则创建
parentFile.mkdirs();
}
for(int i=0;i<image.length;i++){
File saveFile=new File(parentFile,imageFileName[i]);//parentFile:保存路径,imageFileName:保存文件名
FileUtils.copyFile(image[i], saveFile);//将上传的image复制到saveFile中
}
ActionContext.getContext().put("message", "保存成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
return "tsuccess";
} public File[] getImage() {
return image;
} public void setImage(File[] image) {
this.image = image;
} public String[] getImageFileName() {
return imageFileName;
} public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
} public String[] getImageContentType() {
return imageContentType;
} public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
}

第03步:配置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.action.extension" value="do,action"/> <package name="transform" namespace="/" extends="struts-default">
<action name="list_*" class="com.self.action.FileUpload" method="{1}">
<result name="tsuccess">
/outdata.jsp
</result>
</action>
</package>
</struts>

第04步:编写界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
<form method="post" action="list_uploadFile.action" enctype="multipart/form-data">
文件1:<input type="file" name="image">
文件2:<input type="file" name="image">
文件3:<input type="file" name="image">
<BR>
<input type="submit" value="上传文件">
</form>
</body>
</html>

第05步:注意事项

注意事项请参考:struts文件上传(单文件)

第06步:导入包

struts文件上传(多文件)的更多相关文章

  1. SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库

    SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库  /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...

  2. struts2文件上传,文件类型 allowedTypes

    struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...

  3. SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...

  4. webAPI文件上传时文件过大404错误的问题

    背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...

  5. struts2实现文件上传(多文件上传)及下载

    一.要实现文件上传,需在项目中添加两个jar文件 二.上传准备的页面 注:必须植入enctype="multipart/form-data"属性,以及提交方式要设置成post &l ...

  6. Struts2 单个文件上传/多文件上传

    1导入struts2-blank.war所有jar包:\struts-2.3.4\apps\struts2-blank.war 单个文件上传 upload.jsp <s:form action= ...

  7. Struts2之文件上传(单文件/多文件)

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

  8. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  9. ThinkPHP3.2.3多文件上传,文件丢失问题的解决

    描述 thinkphp多文件上传时,有些时候会出现文件丢失的情况.比如上传多个图片,最终只上传了一个图片.本地测试的时候是正常的,但上传到服务器上就会出现丢失文件这种情况. 原因 查看tp上传类(Th ...

  10. Spring mvc 文件上传到文件夹(转载+心得)

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

随机推荐

  1. 回调的代理(delegate)实现

    1.CoreManage.h #import <Foundation/Foundation.h> @protocol SampleProtocol; //声明核心类的属性和方法 @inte ...

  2. 【C51】单片机中断

    引言 其实人的一生和单片机的运行很类似.就拿人的一生来说:有些事只需要做一次,比如得了水痘以后,体内产生免疫,以后就不会再生这个病了.有些事需要反复做,比如反复读书,反复工作,反复与困苦打交道,反复地 ...

  3. HDFS的联盟Federation

    一:概述 1.单个namenode的局限性 namespace的限制 单个namenode所能存储的对象受到JVM中的heap size的限制 namenode的扩张性 不可以水平扩张 隔离性 单个n ...

  4. java的transient

    今天遇到个了一个问题,在使用Swing的drap and drop处理JTree的时候,我的TreeNode中的UserObject中引用了java.awt.Image类. 然后在dnd的时候会报ja ...

  5. springmvc返回值、数据写到页面、表单提交、ajax、重定向

    实验是在前一篇文章的项目上做的: 数据写到页面 后台往前台传数据 TestController添加 /** * 方法的返回值采用ModelAndView, new ModelAndView(" ...

  6. 谈谈.NET中常见的内存泄露问题——GC、委托事件和弱引用

    其实吧,内存泄露一直是个令人头疼的问题,在带有GC的语言中这个情况得到了很大的好转,但是仍然可能会有问题.一.什么是内存泄露(memory leak)?内存泄露不是指内存坏了,也不是指内存没插稳漏出来 ...

  7. 去除undefined和末尾逗号及把字符串数字转成数字数组的方法

    function removeundefined(str){   var v=new Array(),b="";   var tmp=fil(str); for(var i=0;i ...

  8. iOS Block传值

    上个月,针对block恶补了一下,以为自己全部掌握了,其实不尽然. 昨天项目中在下载的时候用到,自己竟然不知道该从何下手,惭愧~ 情景是这个样子的:我写了个下载类,阴老师在调用时,将参数(sid,UR ...

  9. synchronized锁自旋

    http://www.jianshu.com/p/5dbb07c8d5d5 原理 通常说的synchronized在方法或块上加锁,这里的锁就是对象锁(当然也可以在类上面),或者叫重量锁,在JVM中又 ...

  10. weizmann数据库

    http://www.wisdom.weizmann.ac.il/~vision/SpaceTimeActions.html