SpringBoot 最新教程:SpringBoot 2.7 实现文件上传,图片上传并显示功能-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot + Web 项目搭建及实践应用-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot + Web 项目搭建,异常捕获处理-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot + Mysql 项目应用-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 集成 jsp 页面开发 -2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 实现文件上传,图片上传并显示功能-2022年最新图文版本

SpringBoot 2.7教程:springboot 设置全局字符编码,解决乱码问题-2022年最新图文版

SpringBoot 2.7教程:SpringBoot mybatis 多数据源的整合方法-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 RocketMQ 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 RabbitMQ 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 MongoDB 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 Redisson 项目搭建-2022年最新图文版本

SpringBoot 2.7教程:SpringBoot 整合 Redis 项目搭建-2022年最新图文版本

本章主要目标:

1.学习SpringBoot 实现文件上传功能的项目搭建

目录

一、新建springboot-file项目

二、修改pom.xml文件,确定引入的包

三、配置视图解析器

四、添加webapp添加到resource

五、在webapp下添加file.jsp页面

六、添加FileController

七、创建 MyWebMvcConfigurer,配置资源映射路径


一、新建springboot-file项目

二、修改pom.xml文件,确定引入的包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 引入SpringBoot内嵌Tomcat对jsp的解析依赖,不添加这个解析不了jsp -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

同时,在pom文件中的build标签下,添加如下内容:

<!--
    SpringBoot项目默认推荐使用的前端引擎是thymeleaf
    现在我们要使用SpringBoot集成jsp,手动指定jsp最后编译的路径
    同时SpringBoot集成jsp编译jsp的路径是规定好的:META-INF/resources
-->
<resources>
    <resource>
        <!-- 源文件夹 -->
        <directory>src/main/webapp</directory>
        <!-- 指定编译到的路径为 META-INF/resources -->
        <targetPath>META-INF/resources</targetPath>
        <!-- 指定源文件夹中的哪些资源需要进行编译 -->
        <includes>
            <include>*.*</include>
        </includes>
     </resource>
</resources>

三、配置视图解析器

要使用jsp,我们也可以在它的核心配置文件 application.properties 中,声明一下视图解析器。

#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

四、添加webapp添加到resource

五、在webapp下添加file.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/fileUpload" method="post" enctype="multipart/form-data">
    <label>上传图片</label>
    <input type="file" name="file"/>
    <input type="submit" value="上传"/>
</form>
<p>图片:</p>
<img src="${filename }"/>
</body>
</html>

六、添加FileController

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;
/***
 * @date: 2022/6/30
 * @author  WX:深入技术架构
 * @description: 文件上传
 */
@Controller
public class FileController {

    @GetMapping(value = "/file")
    public String file() {
        return "file";
    }

    @PostMapping(value = "/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "/tmp/fileupload/"; // 上传后的路径,windows 使用 "D://fileupload//"
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "/fileupload/" + fileName;
        model.addAttribute("filename", filename);
        return "file";
    }
}

七、创建 MyWebMvcConfigurer,配置资源映射路径

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 资源映射路径
 * WX:深入技术架构
 */
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
                                                                                        // "file:D:/temp-rainy/"
        registry.addResourceHandler("/fileupload/**").addResourceLocations("file:/tmp/fileupload/");
    }
}

测试一下结果:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值