• springboot上传文件到后台指定文件夹


    第一步,在application.yml做一下配置,预设下载目录

    1. files:
    2. upload:
    3. path: D:/SpringBootItem/springboot/files/

    其中有用到hutool工具依赖,如下在pom.xml中添加依赖,也可以选择不添加,自己修改下Controller中的代码即可

    1. cn.hutool
    2. hutool-all
    3. 5.7.20

    第二步,新建一个Controller,实现上传到后台服务器指定文件夹。

    1. package com.example.springboot.controller;
    2. /**
    3. * mybatis代码生成器配置
    4. */
    5. import cn.hutool.core.io.FileUtil;
    6. import cn.hutool.core.util.IdUtil;
    7. import org.springframework.beans.factory.annotation.Value;
    8. import org.springframework.web.bind.annotation.*;
    9. import javax.annotation.Resource;
    10. import java.io.File;
    11. import java.io.IOException;
    12. import com.example.springboot.service.IUploadLogService;
    13. import org.springframework.web.bind.annotation.RestController;
    14. import org.springframework.web.multipart.MultipartFile;
    15. /**
    16. *

    17. * 上传文件
    18. */
    19. @RestController
    20. @RequestMapping("/file")
    21. public class UploadLogController {
    22. /**
    23. * 上传到本地磁盘地址【从application.yml中获取预设置地址参数】
    24. */
    25. @Value("${files.upload.path}")
    26. private String fileUploadPath;
    27. @PostMapping("/upload")
    28. public String upload (@RequestParam MultipartFile file) throws IOException {
    29. // 获取文件参数
    30. String originalFilename = file.getOriginalFilename();
    31. String type = FileUtil.extName(originalFilename);
    32. long size = file.getSize();
    33. File uploadFiletest = new File(fileUploadPath);
    34. // 判断是否不存在该目录,如果是,新建一个该目录
    35. if (!uploadFiletest.exists()) {
    36. uploadFiletest.mkdirs();
    37. }
    38. // 定义一个文件的唯一标识码
    39. String uuid = IdUtil.fastSimpleUUID();
    40. // 重新拼接
    41. File uploadFile = new File(fileUploadPath + uuid + "." + type);
    42. // 进行存储到磁盘
    43. file.transferTo(uploadFile);
    44. return "1";
    45. }
    46. }

    效果

     

    第三步、配置阿里云oss的依赖

    1. com.aliyun.oss
    2. aliyun-sdk-oss
    3. 3.15.1

    第四步、写一个阿里云OSS处理的工具类(核心),配置相应的阿里云oss账号信息,如何设置阿里云OSS账号及获取账号信息,可以去阿里云官方文档查看,如下:

    如何使用STS以及签名URL临时授权访问OSS资源(Java)_对象存储 OSS-阿里云帮助中心 (aliyun.com)

    1. package com.example.springboot.utils;
    2. import com.aliyun.oss.OSS;
    3. import com.aliyun.oss.OSSClientBuilder;
    4. import org.springframework.beans.factory.annotation.Value;
    5. import java.io.ByteArrayInputStream;
    6. /**
    7. * 阿里云OSS工具类
    8. */
    9. public class AliOssUtil {
    10. // endpoint填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou.aliyuncs.com。
    11. public static final String endpoint = "oss-cn-hangzhou.aliyuncs.com";
    12. // 阿里云账号RAM用户进行API访问或日常运维(账号)。
    13. public static final String accessKeyId = "************************";
    14. // 阿里云账号RAM用户进行API访问或日常运维(密钥)。
    15. public static final String accessKeySecret = "***************************";
    16. // bucket名
    17. public static String bucket = "bucketName";
    18. // 连接阿里云OSS
    19. public static OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    20. /**
    21. * 阿里云OSS上传文件
    22. * @param data
    23. * @param url
    24. * @return
    25. */
    26. public static String uploadFile(byte[] data, String url) {
    27. try{
    28. ossClient.putObject(bucket, url, new ByteArrayInputStream(data));
    29. return "true";
    30. } catch (Exception e) {
    31. System.out.println(e);
    32. return "false";
    33. }
    34. }
    35. /**
    36. * 阿里云OSS删除文件
    37. * @param url
    38. * @return
    39. */
    40. public static String delFile(String url) {
    41. try{
    42. ossClient.deleteObject(bucket, url);
    43. return "true";
    44. } catch (Exception e) {
    45. System.out.println(e);
    46. return "false";
    47. }
    48. }
    49. }

    第五步、重新回到第二步的controller接口类进行引用oss工具类的上传和删除方法即可。

    1. package com.example.springboot.controller;
    2. /**
    3. * mybatis代码生成器配置
    4. */
    5. import cn.hutool.core.io.FileUtil;
    6. import cn.hutool.core.util.IdUtil;
    7. import com.aliyun.oss.common.utils.IOUtils;
    8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    9. import com.example.springboot.entity.UploadLog;
    10. import com.example.springboot.utils.AliOssUtil;
    11. import org.springframework.beans.factory.annotation.Value;
    12. import org.springframework.web.bind.annotation.*;
    13. import javax.annotation.Resource;
    14. import java.io.File;
    15. import java.io.FileInputStream;
    16. import java.io.IOException;
    17. import java.util.List;
    18. import com.example.springboot.service.IUploadLogService;
    19. import org.springframework.web.bind.annotation.RestController;
    20. import org.springframework.web.multipart.MultipartFile;
    21. /**
    22. *

    23. * 上传日志 前端控制器
    24. *

    25. *
    26. * @author Sca_jie
    27. * @since 2023-09-13
    28. */
    29. @RestController
    30. @RequestMapping("/file")
    31. public class UploadLogController {
    32. /**
    33. * 自定义controller生成模板
    34. */
    35. @Resource
    36. private IUploadLogService uploadLogService;
    37. /**
    38. * 上传到本地磁盘地址【从application.yml中获取预设置地址参数】
    39. */
    40. @Value("${files.upload.path}")
    41. private String fileUploadPath;
    42. /**
    43. * 上传文件
    44. * @param file 文件(form-data类型,参数名file)
    45. * @return
    46. * @throws IOException
    47. */
    48. @PostMapping("/upload")
    49. public String upload (@RequestParam MultipartFile file) throws IOException {
    50. // 获取文件参数
    51. String originalFilename = file.getOriginalFilename();
    52. String type = FileUtil.extName(originalFilename);
    53. long size = file.getSize();
    54. File uploadFiletest = new File(fileUploadPath);
    55. // 判断是否不存在该目录,如果是,新建一个该目录
    56. if (!uploadFiletest.exists()) {
    57. uploadFiletest.mkdirs();
    58. }
    59. // 定义一个文件的唯一标识码
    60. String uuid = IdUtil.fastSimpleUUID();
    61. // 重新拼接
    62. File uploadFile = new File(fileUploadPath + uuid + "." + type);
    63. // 进行存储到磁盘
    64. file.transferTo(uploadFile);
    65. // 读取文件
    66. FileInputStream fileInputStream = new FileInputStream(fileUploadPath + uuid + "." + type);
    67. byte[] data = IOUtils.readStreamAsByteArray(fileInputStream);
    68. // 上传阿里云OSS
    69. String res = AliOssUtil.uploadFile(data, "upload/2023-09/" + uuid + "." + type);
    70. return res;
    71. }
    72. /**
    73. * 删除阿里云OSS文件
    74. * @param url
    75. * @return
    76. */
    77. @PostMapping("/delfile")
    78. public String delFile (@RequestParam String url) {
    79. System.out.println(url);
    80. String res = AliOssUtil.delFile(url);
    81. return res;
    82. }
    83. }

     效果如下:

  • 相关阅读:
    秋招每日一题T23——连通图
    软硬件架构分层总结
    SpringCloud 学习笔记总结 (二)
    元宇宙vr工业产品展示空间降低研发成本
    防火墙nat策略实验和多出口实验和智能选路实验
    AJAX学习笔记3练习
    构建高效的BFF(Backend for Frontend):优化前端与后端协作
    文件上传16.17关
    GPT引领前沿与应用突破之GPT4科研实践技术与AI绘图
    基于SpringBoot前后端分离的网吧管理系统
  • 原文地址:https://blog.csdn.net/weixin_42966151/article/details/132853973