struts2实现多文件上传的示例代码

本文介绍如何使用Struts2实现多文件上传功能,包括配置struts.xml文件、创建上传控制器类FileUpload及搭建上传表单。通过具体代码展示了文件上传过程中的配置细节。
 本篇文章主要介绍了struts2实现多文件上传的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

开发环境JDK1.8 eclipse struts2-2.3.31 

1.创建web项目 

2.导入struts2核心jar包 

3.更改web.xml配置文件(只要配置好struts2的Filter就好) 

4.创建src/struts.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
< struts >
   <!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
     如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
   < constant name = "struts.action.extension" value = "do" />
   <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
   < constant name = "struts.serve.static.browserCache" value = "false" />
   <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
   < constant name = "struts.configuration.xml.reload" value = "true" />
   <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
   < constant name = "struts.devMode" value = "true" />
   <!-- 默认的视图主题 -->
   < constant name = "struts.ui.theme" value = "simple" />
   <!--<constant name="struts.objectFactory" value="spring" />-->
   <!--解决乱码  -->
   < constant name = "struts.i18n.encoding" value = "UTF-8" />
   <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->
   < constant name = "struts.multipart.maxSize" value = "10701096" />
   <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir -->
   < constant name = "struts.multipart.saveDir " value = "d:/tmp" />
 
   < package name = "upload" extends = "struts-default" >
     < action name = "fileUpload" class = "com.ifan.action.FileUpload" >
       <!-- 动态设置savePath的属性值 -->
       < param name = "savePath" >WEB-INF/images</ param >
       < result name = "success" >/success.jsp</ result >
       < result name = "input" >/error.jsp</ result >
       < interceptor-ref name = "fileUpload" >
         <!-- 文件过滤 -->
         < param name = "allowedTypes" >image/bmp,image/png,image/gif,image/jpeg</ param >
         <!-- 文件大小, 以字节为单位 -->
         < param name = "maximumSize" >1025956</ param >
       </ interceptor-ref >
       <!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
       < interceptor-ref name = "defaultStack" />
     </ action >
   </ package >
</ struts >

5.创建src/com.ifan.action.FileUpload.Java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.ifan.action;
 
import java.io.File;
 
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
public class FileUpload extends ActionSupport{
 
   private File[] image; //上传的文件
   private String[] imageFileName; //文件名称
   private String[] imageContentType; //文件类型
 
   public String execute() throws Exception {
     ServletActionContext.getRequest().setCharacterEncoding( "UTF-8" );
     String realpath = ServletActionContext.getServletContext().getRealPath( "/images" );
     System.out.println(realpath);
     if (image != null ) {
       File savedir= new File(realpath);
       if (!savedir.getParentFile().exists())
         savedir.getParentFile().mkdirs();
       for ( int i= 0 ;i<image.length;i++){
         File savefile = new File(savedir, imageFileName[i]);
         FileUtils.copyFile(image[i], savefile);
       }
       ActionContext.getContext().put( "message" , "文件上传成功" );
     }
     return "success" ;
   }
 
   public File[] getImage() {
     return image;
   }
 
   public void setImage(File[] image) {
     this .image = image;
   }
 
   public String[] getImageContentType() {
     return imageContentType;
   }
 
   public void setImageContentType(String[] imageContentType) {
     this .imageContentType = imageContentType;
   }
 
   public String[] getImageFileName() {
     return imageFileName;
   }
 
   public void setImageFileName(String[] imageFileName) {
     this .imageFileName = imageFileName;
   }
}

6.创建WebContent/index.jsp ,作为上传文件的页面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<%@ 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%>" rel= "external nofollow" >
 
<title>My JSP 'hello.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" rel= "external nofollow" >
   -->
 
</head>
 
<body>
<!-- Struts2的文件上传标签 -->
<s:form action= "fileUpload" namespace= "/" method= "POST" enctype= "multipart/form-data" >
<!-- 该name需要和后台的File类型的名字对应起来,否则将得不到该文件 size 上传文件的大小 -->
<s:file name= "image" label= "Select a File to upload" size= "40" />
<s:file name= "image" label= "Select a File to upload" size= "40" />
<s:submit value= "submit" name= "submit" />
 
</s:form>
 
</body>
</html>

7.创建WebContent/success.jsp 作为文件上传成功跳转的页面,创建WebContent/error.jsp 作为文件上传失败的页面 , 创建WebContent/images文件夹,作为上传文件的存储位置

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

源码来自:https://pan.quark.cn/s/41b9d28f0d6d 在信息技术领域中,jQuery作为一个广受欢迎的JavaScript框架,显著简化了诸多操作,包括对HTML文档的遍历、事件的管理、动画的设计以及Ajax通信等。 本篇文档将深入阐释如何运用jQuery达成一个图片自动播放的功能,这种效果常用于网站的轮播展示或幻灯片演示,有助于优化用户与页面的互动,使网页呈现更加动态的视觉体验。 为了有效实施这一功能,首先需掌握jQuery的核心操作。 通过$符号作为接口,jQuery能够迅速选取DOM组件,例如$("#id")用于选取具有特定ID的元素,而$(".class")则能选取所有应用了某类class的元素。 在选定元素之后,可以执行多种行为,诸如事件监听、样式的变更、内容的更新以及动画的制作等。 关于“一个基于jQuery的图片自动播放功能”,首要任务是准备一组图片素材,这些素材将被整合至一个容器元素之中。 例如,可以构建一个div元素,将其宽度设定为单张图片的尺寸,再借助CSS实现溢出内容的隐藏,从而构建出水平滚动的初始框架。 ```html<div id="slider"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <!-- 更多图片内容... --></div>```接着,需要编写jQuery脚本以实现图片的自动切换。 这通常涉及到定时器的运用,以设定周期性间隔自动更换当前显示的图片。 通过使用`.fadeOut()`和`.fadeIn()`方法,能够实现图片间的平滑过渡,增强视觉效果。 ```javascript$(document).re...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值