广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot上传文件并配置本地资源映射来访问文件的实例代码
  • 925
分享到

SpringBoot上传文件并配置本地资源映射来访问文件的实例代码

2024-04-02 19:04:59 925人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

1.准备工作 1、新建一个SpringBoot项目加上WEB依赖, 所有依赖 <dependency> <groupId>org.springframe

1.准备工作

1、新建一个SpringBoot项目加上WEB依赖, 所有依赖


<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.thymeleaf</groupId>
	<artifactId>thymeleaf-spring5</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

2、application.properties


# 上传文件总的最大值
spring.servlet.multipart.max-request-size=10MB
#spring.servlet.multipart.max-request-size=10KB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB
#spring.servlet.multipart.max-file-size=10KB

# 文件上传路径, 当前项目根目录下的 uploadFile 目录
pro.uploadPath=uploadFile

3、新建文件上传的静态文件 index.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
    <body>
        <h1>文件上传</h1>
        <fORM method="post" action="/file/upload" enctype="multipart/form-data">
            <input type="file" name="file"><br>
            <input type="submit" value="提交">
        </form>
    </body>
</html>

2. 配置本地资源映射路径 addResourceHandlers


package com.pro.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerReGIStry;
import org.springframework.web.servlet.config.annotation.WebmvcConfigurer;

@Configuration
public class WebMvcConf implements WebMvcConfigurer {
    @Value("${pro.uploadPath}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(uploadPath + "
    public String getUploadPath(httpservletRequest request, String fileName) {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + uploadPath + "/" + fileName;
    }

    // 文件上传, 上传到当前项目根目录下的 uploadFile 目录
    @RequestMapping("/file/upload")
    public Object upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
        Map<String, Object> map = new HashMap();
        map.put("msg", "上传成功");

        if (multipartFile.isEmpty()) {
            map.put("msg", "空文件");
            return map;
        }

        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        if ("".equals(fileName)) {
            map.put("msg", "文件名不能为空");
            return map;
        }
        System.out.println("上传文件原始的名字: " + fileName);

        // 使用uuid生成新文件名
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + fileName.substring(fileName.lastIndexOf("."), fileName.length());
        System.out.println("保存的文件的新名字: " + newFileName);

        // 获取年月日的日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());

        // 生成以日期分割的文件路径
        File readPath = new File(uploadPath + File.separator + format);
        System.out.println("存放的文件夹: " + readPath);
        System.out.println("存放文件的绝对路径: " + readPath.getAbsolutePath());
        // 判断文件夹是否存在
        if (!readPath.isDirectory()) {
            // 创建文件夹
            readPath.mkdirs();
        }

        // 文件真实的保存路径
        File file = new File(readPath.getAbsolutePath() + File.separator + newFileName);
        try {
            multipartFile.transferTo(file);

            // 获取存储路径
            String filePath = getUploadPath(request, format + "/" + newFileName);
            map.put("path", filePath);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg", "上传失败");
        }

        return map;
    }
}

到此这篇关于SpringBoot上传文件并配置本地资源映射来访问文件的文章就介绍到这了,更多相关SpringBoot上传文件资源映射内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot上传文件并配置本地资源映射来访问文件的实例代码

本文链接: https://www.lsjlt.com/news/124387.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作