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

作者:iFan138 时间:2022-03-09 23:40:54 

开发环境JDK1.8 eclipse struts2-2.3.31

1.创建web项目

2.导入struts2核心jar包

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

4.创建src/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>
 <!-- 该属性指定需要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


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 ,作为上传文件的页面


<%@ 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文件夹,作为上传文件的存储位置

来源:http://blog.csdn.net/ifan138/article/details/61206275

标签:struts,文件,上传
0
投稿

猜你喜欢

  • C#语法相比其它语言比较独特的地方(二)

    2021-12-20 20:56:03
  • SpringBoot @Validated注解实现参数分组校验的方法实例

    2023-01-02 11:18:50
  • spring注解之@Valid和@Validated的区分总结

    2023-11-01 07:51:42
  • Java Code Cache满导致应用性能降低问题解决

    2021-06-18 18:17:56
  • Java并发编程学习之Unsafe类与LockSupport类源码详析

    2022-10-21 19:07:01
  • JavaMailSender实现邮箱验证功能

    2022-05-11 12:32:59
  • java实现滑动验证解锁

    2023-06-02 12:16:36
  • 详解如何让Spring MVC显示自定义的404 Not Found页面

    2023-12-12 15:48:47
  • C# DataGridView绑定数据源的方法

    2023-06-30 00:26:39
  • Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件

    2023-07-06 01:24:29
  • java实现文件下载的两种方式

    2023-11-11 06:37:14
  • C# 获取IP及判断IP是否在区间

    2021-10-06 23:44:50
  • C#利用ZXing.Net生成条形码和二维码

    2023-11-04 00:36:02
  • 有关tomcat内存溢出的完美解决方法

    2023-09-18 09:02:25
  • RSA密钥--JAVA和C#的区别及联系

    2022-09-18 12:16:44
  • Java从同步容器到并发容器的操作过程

    2021-10-14 05:26:58
  • c# 复写Equals方法的实现

    2023-06-09 01:35:35
  • Spring+EHcache缓存实例详解

    2023-12-12 20:27:51
  • Java Calendar类使用案例详解

    2023-07-09 14:03:22
  • spring-boot-maven-plugin报红解决方案(亲测有效)

    2022-07-23 01:16:46
  • asp之家 软件编程 m.aspxhome.com