使用springboot简单实现上传多张图片的功能

该博客详细介绍了如何使用SpringBoot实现多张图片上传的功能。首先在后台配置文件中设置了上传路径,并在pom.xml中进行了相关依赖配置。接着在前端使用Thymeleaf语法处理图片路径。最后通过访问特定URL可以展示上传结果。

使用springboot简单实现上传多张图片的功能

后台

@Controller
public class IndexController {

    @Value("${my-config.file-path}")
    private String myFilePath;

    @RequestMapping("test")
    public String test() {
        return "index";
    }

    @RequestMapping("uploadfiles")
    public String uploadfiles(@RequestParam("uploadfiles") MultipartFile[] uploadfiles,Model model) throws Exception{
        List<String> list = new ArrayList<>();
        for(int i=0;i<uploadfiles.length;i++){
            String prefix = uploadfiles[i].getOriginalFilename().split("\\.")[0];
            String suffix = uploadfiles[i].getOriginalFilename().split("\\.")[1];
            String newfilename = prefix+System.currentTimeMillis()+"."+suffix;
            File newFile = new File(myFilePath+"uploaded"+File.separator+newfilename);
            if(!newFile.getParentFile().exists()){
                newFile.getParentFile().mkdirs();
            }
            uploadfiles[i].transferTo(newFile);
            list.add("uploaded/"+newfilename);
        }
        model.addAttribute("list",list);
        return "index";
    }
}

配置文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

如下代码的意思是它会解析前端的图片路径并在“D://uploaded/”这个目录下去找相应的静态资源

@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/uploaded/**").
                addResourceLocations("file:/" + "D://uploaded/");
    }
}

设置端口和文件的默认上传路径
在这里插入图片描述

前台

前端使用的是thymeleaf语法
以th:开头的标签不会在浏览器上展示,只有当后端返回数据后,才会覆盖原始数据

<h3>多文件上传</h3>
    <form action="/uploadfiles" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadfiles"  multiple/>
        <br>
        <input type="submit" value="多文件上传"/>
    </form>
    <div th:each="item: ${list}" >
        [[${item}]]
    </div>
    <div th:each="item: ${list}" >
        <img src="../img/120200428150822131.PNG" th:src="@{${item}}" alt="图片" >
        <hr>
    </div>

结果展示

访问http://localhost:8899/test
在这里插入图片描述

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值