Struts2实现多文件上传功能

作者:Giving_bestself 时间:2021-11-01 13:50:25 

前台form 表单:设置method=post,enctype=multipart/form-data。

struts2在原有的上传解析器继承上做了进一步封装,更进一步简化了文件上传。

Action需要使用3个属性来封装该文件域的信息:

(1)类型为File的*属性封装了该文件域对应的文件内容;
(2)类型为String的***FileName属性封装了该文件域对应的文件的文件类型;
(3)类型为String的***ContentType属性封装了该文件域对应的文件的类型。

具体实现:

新建web项目

Struts2实现多文件上传功能

添加struts2相关包
myeclipse可直接下载,右击项目,如下。

Struts2实现多文件上传功能

前台


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
 <form action="upload.action" method="post" enctype="multipart/form-data">
   <input type="file" name="upload" multiple="multiple"/>
   <input type="submit" value="提交"/>

</form>
</body>
</html>

配置web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>UploadFile</display-name>
<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>*.action</url-pattern>
</filter-mapping>
</web-app>

配置struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
 <constant name="struts.devMode" value="true"/>
 <constant name="struts.multipart.saveDir" value="/tmp"/>
 <constant name="struts.custom.i18n.resources" value="app"></constant>

<package name="default" namespace="/" extends="struts-default">
   <action name="upload" class="com.yf.action.UploadAction">
   <result>/index.jsp</result>
   <interceptor-ref name="defaultStack"></interceptor-ref>
   </action>
 </package>
</struts>  

后台代码


public class UploadAction extends ActionSupport{
 private List<File> upload;
 private List<String> uploadContentType;
 private List<String> uploadFileName;

public List<File> getUpload() {
   return upload;
 }

public void setUpload(List<File> upload) {
   this.upload = upload;
 }

public List<String> getUploadContentType() {
   return uploadContentType;
 }

public void setUploadContentType(List<String> uploadContentType) {
   this.uploadContentType = uploadContentType;
 }

public List<String> getUploadFileName() {
   return uploadFileName;
 }

public void setUploadFileName(List<String> uploadFileName) {
   this.uploadFileName = uploadFileName;
 }

@Override
 public String execute() throws Exception {
   //文件保存路径
   String path = ServletActionContext.getServletContext().getRealPath("/images");
   File file = new File(path);
   //不存在则创建
   if(!file.exists()){
     file.mkdir();
   }
   //循环将文件上传到指定路径
   for(int i = 0; i< upload.size(); i++){
     FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
   }
   return SUCCESS;
 }

结果如下

Struts2实现多文件上传功能

Struts2实现多文件上传功能

来源:http://blog.csdn.net/giving_bestself/article/details/77513177

标签:Struts2,上传
0
投稿

猜你喜欢

  • Java 发送http请求(get、post)的示例

    2022-02-15 12:20:56
  • JDK线程池和Spring线程池的使用实例解析

    2023-02-24 13:27:32
  • C#中的委托、事件学习笔记

    2023-01-21 18:03:49
  • 一文搞懂MyBatis多数据源Starter实现

    2023-07-19 03:34:22
  • Java提取2个集合中的相同和不同元素代码示例

    2023-11-28 05:48:41
  • IDEA中的.iml文件和.idea文件夹

    2023-11-23 11:47:19
  • El表达式使用问题javax.el.ELException:Failed to parse the expression的解决方式

    2023-11-24 12:47:13
  • Android实现摇一摇功能

    2023-07-23 20:21:11
  • Flutter瀑布流仿写原生的复用机制详解

    2023-06-20 17:02:08
  • SpringSecurity解决POST方式下CSRF问题

    2023-07-18 18:59:51
  • Java中初始化List集合的八种方式汇总

    2021-09-20 22:31:54
  • Spring Cloud Hystrix 服务降级限流策略详解

    2022-05-02 15:20:27
  • Lombok为啥这么牛逼?SpringBoot和IDEA官方都要支持它

    2021-10-18 23:04:50
  • Spring注解@Configuration与@Bean注册组件的使用详解

    2022-09-13 01:52:56
  • java编程之基于SpringBoot框架实现扫码登录

    2023-02-14 02:39:28
  • 面试官:详细谈谈Java对象的4种引用方式

    2022-09-19 04:52:20
  • Spring探秘之如何妙用BeanPostProcessor

    2021-07-01 22:41:46
  • IDEA怎么生成UML类图的实现

    2023-12-07 15:40:21
  • ShardingSphere jdbc集成多数据源的实现步骤

    2023-11-25 07:54:56
  • android6.0权限动态申请框架permissiondispatcher的方法

    2023-07-31 10:51:57
  • asp之家 软件编程 m.aspxhome.com