struts文件上传(多文件)
第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文件上传(多文件)的更多相关文章
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- webAPI文件上传时文件过大404错误的问题
背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...
- struts2实现文件上传(多文件上传)及下载
一.要实现文件上传,需在项目中添加两个jar文件 二.上传准备的页面 注:必须植入enctype="multipart/form-data"属性,以及提交方式要设置成post &l ...
- Struts2 单个文件上传/多文件上传
1导入struts2-blank.war所有jar包:\struts-2.3.4\apps\struts2-blank.war 单个文件上传 upload.jsp <s:form action= ...
- Struts2之文件上传(单文件/多文件)
<一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...
- spring mvc文件上传(单个文件上传|多个文件上传)
单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包 1.所需jar包: commons-fileupload-1.3.1.jar ...
- ThinkPHP3.2.3多文件上传,文件丢失问题的解决
描述 thinkphp多文件上传时,有些时候会出现文件丢失的情况.比如上传多个图片,最终只上传了一个图片.本地测试的时候是正常的,但上传到服务器上就会出现丢失文件这种情况. 原因 查看tp上传类(Th ...
- Spring mvc 文件上传到文件夹(转载+心得)
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
随机推荐
- 验证进入AppStore的评分界面
NSString * appstoreUrlString = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/view ...
- qt cef嵌入web
原文http://blog.sina.com.cn/s/blog_9e59cf590102vnfc.html 最近项目需要,研究了下libcef库. Cef(Chromium Embedded Fra ...
- C/C++ 获取汉字拼音首字母
#include <stdint.h> #include <stdio.h> #include <ctype.h> #include <string.h> ...
- angularJS中controller的通信
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- linux安装pip报错
解决方法:升级pip工具 sudo python -m pip install -U pip
- H3C交换机配置
h3c 交换机的配置命令 通过 console 连接到交换机 交换机所使用的 console 接口看上去像是一个普通的 RJ45 网卡接口,但是并不能使用普通的网线与 PC 连接 ^_^ .它要通过 ...
- UIStoryboard类介绍(如何从Storyboard中加载View Controller)
如何从Storyboard中加载View Controller? 1. 首先了解下UIStoryboard类: @class UIViewController; @interface UIStoryb ...
- 面向对象编程(九)——面向对象三大特性之继承以及重写、Object类的介绍
面向对象三大特性 面向对象三大特征:继承 :封装/隐藏 :多态(为了适应需求的多种变化,使代码变得更加通用!) 封装:主要实现了隐藏细节,对用户提供访问接口,无需关心方法的具体实现. 继承:很好的实现 ...
- CNContact对通讯录的基本使用(第二篇)
/** * 注意:iOS9才有能使用 * 首先在工程里导入ContactsUI.framework和Contacts.framework两个框架 * * * 源代码的链接地址 * 链接: http:/ ...
- cocos2dx 3.x以上版本搭建Mac环境(百分百可行)
近期由于工作的原因,有机会接触了游戏行业,说实话,本人学程序最原始的初衷就是想做游戏,于是就创建了一篇cocos2d-x的分类来记录我在学习cocos2d-x的成长过程. 首先第一篇,想学cocos2 ...