SpringMVC实现多文件上传

作者:吴声子夜歌 时间:2023-10-12 16:49:15 

本文实例为大家分享了Spring MVC多文件上传的具体代码,供大家参考,具体内容如下

1)创建工程并导入JAR包

SpringMVC实现多文件上传

SpringMVC实现多文件上传

2)创建多文件选择页面

在 WebContent 目录下创建 JSP 页面 multiFiles.jsp,在该页面中使用表单上传多个文件,具体代码如下:


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/multifile"
 method="post" enctype="multipart/form-data">
 选择文件1:<input type="file" name="myfile"><br>
 文件描述1:<input type="text" name="description"><br />
 选择文件2:<input type="file" name="myfile"><br>
 文件描述2:<input type="text" name="description"><br />
 选择文件3:<input type="file" name="myfile"><br>
 文件描述3:<input type="text" name="description"><br />
 <input type="submit" value="提交">
</form>
</body>
</html>

3)创建POJO类


package pers.zhang.pojo;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
public class MultiFileDomain {
private List<String> description;
private List<MultipartFile> myfile;

public List<String> getDescription() {
return description;
}
public void setDescription(List<String> description) {
this.description = description;
}
public List<MultipartFile> getMyfile() {
return myfile;
}
public void setMyfile(List<MultipartFile> myfile) {
this.myfile = myfile;
}

}

4)创建多文件上传处理方法


/**
* 多文件上传
*/
@RequestMapping("/multifile")
public String multiFileUpload(@ModelAttribute MultiFileDomain multiFileDomain,HttpServletRequest request) {
String realpath = request.getServletContext().getRealPath("uploadfiles");
File targetDir = new File(realpath);
if (!targetDir.exists()) {
 targetDir.mkdirs();
}
List<MultipartFile> files = multiFileDomain.getMyFile();
for (int i = 0; i < files.size(); i++) {
 MultipartFile file = files.get(i);
 String fileName = file.getOriginalFilename();
 File targetFile = new File(realpath, fileName);
 // 上传
 try {
  file.transferTo(targetFile);
 } catch (Exception e) {
  e.printStackTrace();
 }
}
logger.info("成功");
return "showMulti";
}

5)创建成功显示页面


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
 <tr>
  <td>详情</td>
  <td>文件名</td>
 </tr>
 <!-- 同时取两个数组的元素 -->
 <c:forEach items="${multiFileDomain.description}" var="description"
  varStatus="loop">
  <tr>
   <td>${description}</td>
   <td>${multiFileDomain.myfile[loop.count-1].originalFilename}</td>
  </tr>
 </c:forEach>
 <!-- fileDomain.getMyfile().getOriginalFilename() -->
</table>
</body>
</html>

6)测试文件上传

SpringMVC实现多文件上传

SpringMVC实现多文件上传

来源:https://blog.csdn.net/cold___play/article/details/102874159

标签:SpringMVC,上传
0
投稿

猜你喜欢

  • 深入剖析Java工厂模式让你的代码更灵活

    2022-05-26 00:42:28
  • Java反射之类的实例对象的三种表示方式总结

    2023-10-03 09:51:35
  • 使用java基础类实现zip压缩和zip解压工具类分享

    2021-11-23 08:03:41
  • Spring Retry 重试实例详解

    2021-07-15 22:43:26
  • WPF+ASP.NET SignalR实现后台通知功能的示例代码

    2021-10-11 04:49:45
  • Java NIO:浅析IO模型_动力节点Java学院整理

    2022-01-17 17:07:38
  • Android下拉刷新控件PullToRefresh实例解析

    2022-01-27 08:01:14
  • Java NIO实现聊天功能

    2022-06-12 08:31:31
  • UnityWebRequest前后端交互实现过程解析

    2021-09-07 01:24:42
  • 使用flutter创建可移动的stack小部件功能

    2023-06-21 12:28:25
  • Java监听器的作用及用法代码示例

    2023-06-24 06:59:07
  • Android实用图文教程之代码混淆、第三方平台加固加密、渠道分发

    2022-06-23 18:12:44
  • Android实现复制Assets文件到SD卡

    2022-03-21 03:36:53
  • C#编写游戏客户端的实现代码

    2021-08-28 06:24:58
  • HashMap原理及手写实现部分区块链特征

    2023-10-15 03:27:27
  • MyBatis查询结果resultType返回值类型的说明

    2021-10-25 21:37:39
  • 浅谈Android开发中项目的文件结构及规范化部署建议

    2022-05-13 12:47:37
  • Java命名规则详细总结

    2023-11-14 12:20:55
  • 详解Spring Boot Junit单元测试

    2021-10-25 23:13:55
  • C#/VB.NET 实现在PDF表格中添加条形码

    2022-12-25 19:58:16
  • asp之家 软件编程 m.aspxhome.com