struts2实现多文件上传
作者:雪飘雪融 发布时间:2023-11-23 02:27:53
标签:struts2,文件上传
本文实例为大家分享了struts2实现多文件上传的具体代码,供大家参考,具体内容如下
首先搭建好struts2的开发环境,导入struts2需要的最少jar包
新建upload.jsp页面,注意一定要把表单的enctype设置成multipart/form-data
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
</head>
<body>
<s:fielderror name="fieldErrors"></s:fielderror>
<s:form theme="simple" action="upload" enctype="multipart/form-data" method="post">
file:<s:file name="file"></s:file>
fileDesc:<s:textfield name="fileDesc[0]"></s:textfield>
<br/><br/>
file:<s:file name="file"></s:file>
fileDesc:<s:textfield name="fileDesc[1]"></s:textfield>
<br/><br/>
file:<s:file name="file"></s:file>
fileDesc:<s:textfield name="fileDesc[2]"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
新建一个UploadAction类,这个类主要有三个属性,并为这三个属性生成对应的set get方法
[File Name] : 保存要上传的文件
[File Name]ContentType : 保存要上传的文件类型
[File Name]FileName :保存上传的文件名
package cn.lfd.web.upload;
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;
/*
* 多文件上传要把对应的属性类型都改为List集合,struts自动会把多个文件的数据封装到里面
*/
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private List<File> file;
private List<String> fileContentType;
private List<String> fileFileName;
private List<String> fileDesc;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileDesc() {
return fileDesc;
}
public void setFileDesc(List<String> fileDesc) {
this.fileDesc = fileDesc;
}
@Override
public String execute() throws Exception {
//遍历文件集合,通过IO流把每一个上传的文件保存到upload文件夹下面
for(int i=0;i<file.size();i++) {
//得到要保存的文件路径
String dir = ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName.get(i));
OutputStream out = new FileOutputStream(dir);
InputStream in = new FileInputStream(file.get(i));
byte[] flush = new byte[1024];
int len = 0;
while((len=in.read(flush))!=-1) {
out.write(flush, 0, len);
}
in.close();
out.close();
}
return "input";
}
}
然后在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="message"></constant>
<package name="default" extends="struts-default">
<interceptors>
<interceptor-stack name="lfdstack">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">200000</param><!-- 限制上传的单个文件的大小 -->
<param name="fileUpload.allowedTypes">text/html,text/xml</param><!-- 限制上传的文件的类型 -->
<param name="fileUpload.allowedExtensions">txt,html,xml</param><!-- 限制上传的文件的扩展名 -->
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="lfdstack"></default-interceptor-ref>
<action name="upload" class="cn.lfd.web.upload.UploadAction">
<result>/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
在src目录下新建一个message.properties文件定制错误消息
struts.messages.error.uploading - 文件不能被上传
struts.messages.error.file.too.large - 文件超出大小
struts.messages.error.content.type.not.allowed - 文件类型不合法
struts.messages.error.file.extension.not.allowed - 文件扩展名不合法
显示效果如下图:
来源:https://blog.csdn.net/qq_22498277/article/details/51345283
0
投稿
猜你喜欢
- idea中的Maven导包失败问题解决总结1.先确定idea和Maven 的配置文件settings 没有问题找到我们本地的maven仓库,
- 容器类、正则表达式在几乎所有编程语言都存在的东西。很常用也很使用。下面用如下的一个控制台小程序说明C#的正则表达式与容器类的应用。开始直接输
- eclipse中改变默然的workspace的方法可以有以下几种:1.在创建project的时候,手动选择使用新的workspace,如创建
- 本文实例讲述了C#实现农历日历的方法。分享给大家供大家参考。具体实现方法如下://天干 private static
- java:使用匿名类直接new接口java中的匿名类有一个倍儿神奇的用法,见下面代码示例:package contract;public i
- 前提最近发现各个频道推荐了很多ULID相关文章,这里对ULID的规范文件进行解读,并且基于Java语言自行实现ULID,通过此实现过程展示U
- 本文实例为大家分享了本地图片或者网络图片高斯模糊效果(毛玻璃效果),具体内容如下首先看效果图1.本地图片高斯模糊2.网络图片高斯模糊gith
- 本文实例讲述了java计算给定字符串中出现次数最多的字母和该字母出现次数的方法。分享给大家供大家参考,具体如下:import Java.ut
- 定时器问题定时器属于基本的基础组件,不管是用户空间的程序开发,还是内核空间的程序开发,很多时候都需要有定时器作为基础组件的支持。一个定时器的
- 编写一个简单的mybatis进行插入数据的实例1 数据库建表 其中建表dob=Date of Birth 的意思create table s
- 前言这几天听朋友说JPA很好用,根本不用写sql。我在想一个程序员不写sql还能叫程序员?而且越高级的工具封装越多的工具,可拓展性和效率就非
- springboot结合maven配置不同环境的profile1、在spring-boot中新建配置文件spring-boot不同环境配置文
- 一、注解(annotations)列表@SpringBootApplication:包含了@ComponentScan、@Configura
- 在Mybatis的动态SQL和${}形式的参数中都用到了OGNL表达式。Mybatis常用的OGNL表达式如下1、e1 or e2:或<
- 本文实例讲述了java设计模式之工厂模式。分享给大家供大家参考,具体如下:工厂模式(factory)涉及到4个角色:抽象工厂类角色,具体工厂
- 引言 在多线程中,为了使线程安全,我们经常会使用synchronized和Lock进行代码同步和加锁,但是具体两者有什么区别,什
- 本文实例讲述了Android ListView的简单应用。分享给大家供大家参考,具体如下:我们今天要讲的内容是Android中ListVie
- 本文实例为大家分享了java计算工作时间的具体代码,不包括节假日、双休日,供大家参考,具体内容如下package common.util;
- Spring中BeanFactory FactoryBean和ObjectFactory的三种的区别引言关于FactoryBean 和 Be
- 在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户