目录
一、前端页面
二、controller层
三、遍历集合回显
一、前端页面
通过表单方式提交?别忘了引入JS文件
?
<%--
Created by IntelliJ IDEA.
User: jdy12
Date: 2021/12/7
Time: 20:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/load5" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"/><br><br>
<input type="file" name="myfile"/><br><br>
<input type="file" name="myfile"/><br><br>
<input type="file" name="myfile"/><br><br>
<input type="file" name="myfile"/><br><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
二、controller层
遍历数组存储?并把图片url链接添加到list集合中方便回显
package com.exy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @program: SpringFile
* @description:
* @author: jdy
* @create: 2021-12-07 20:13
**/
@Controller
public class LoadController5 {
@RequestMapping("/load5")
public String upload01(MultipartFile[] myfile, HttpServletRequest request){
String path = request.getSession().getServletContext().getRealPath("/load");
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
List list = new ArrayList();
for (int i = 0; i <myfile.length ; i++) {
String filename = myfile[i].getOriginalFilename();
File target = new File(path + "/" + filename);
try {
myfile[i].transferTo(target);
} catch (IOException e) {
e.printStackTrace();
}
String url="http://localhost:8080/SpringFile/load/"+filename;
list.add(url);
}
request.setAttribute("list",list);
System.out.println(list);
return "success";
}
}
三、遍历集合回显
添加依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
</dependency>
?
<%--
Created by IntelliJ IDEA.
User: jdy12
Date: 2021/12/7
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div style="border: red;width: 1000px;height: 800px;">
<c:forEach items="${list}" var="url">
<img src="${url}"/>
</c:forEach >
</div>
</body>
</html>
|