前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 本地文件上传及提供HTTP访问服务

Spring Boot 本地文件上传及提供HTTP访问服务

原创
作者头像
HLee
修改2021-08-19 18:02:24
1.8K0
修改2021-08-19 18:02:24
举报
文章被收录于专栏:房东的猫房东的猫房东的猫

简介

服务端接收上传的目的是提供文件的访问服务,那么对于SpringBoot而言,可以提供文件访问的静态资源目录:

  • classpath:/META-INF/resources/ ,
  • classpath:/static/ ,
  • classpath:/public/ ,
  • classpath:/resources/

上传目录自定义配置

Spring boot 为我们提供了使用spring.resources.static-locations配置自定义静态文件的位置。

web:
  upload-path: /Users/booker/Documents/projects/git/master/source

spring:
  resources:
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
  • 配置web.upload-path为与项目代码分离的静态资源路径,即:文件上传保存根路径
  • 配置spring.resources.static-locations,除了带上Spring Boot默认的静态资源路径之外,加上file:${web.upload-path}指向外部的文件资源上传路径。该路径下的静态资源可以直接对外提供HTTP访问服务。

文件上传实现

@RestController
@RequestMapping(value = "/fileUpload")
public class FileUploadController {

    /**
     * 绑定文件上传路径到uploadPath
     */
    @Value("${web.upload-path}")
    private String uploadPath;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest request) {

        // 在 uploadPath 文件夹中通过日期对上传的文件归类保存
        // 比如:/2019/06/06/cf13891e-4b95-4000-81eb-b6d70ae44930.png
        String format = sdf.format(new Date());
        File folder = new File(uploadPath);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }

        // 对上传的文件重命名,避免文件重名
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            // 文件保存
            uploadFile.transferTo(new File(folder, newName));

            // 返回上传文件的访问路径  http://localhost:2000//spring-master/85e4fcac-c903-4a7d-bdce-b155b4354402.jpeg
            String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +"/" + newName;
            return filePath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

模拟的文件上传页面

把该upload.html文件放到classpath:public目录下,对外提供访问。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/spring-master/fileUpload/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadFile" value="请选择上传文件">
        <input type="submit" value="保存">
    </form>
</body>
</html>

访问:http://localhost:2000/spring-master/upload.html

文件被保存到服务端的web.upload-path指定的资源目录下

浏览器端响应结果如下,返回一个文件HTTP访问路径:http://localhost:2000/spring-master/0968094e-7332-4705-9893-1884d42a5028.jpeg

使用该HTTP访问路径,在浏览器端访问效果如下。证明我们的文件已经成功上传到服务端,以后需要访问该图片就通过这个HTTP URL就可以了。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 上传目录自定义配置
  • 文件上传实现
  • 模拟的文件上传页面
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档