Springboot在服务端或者jar里面可以进行仿FTP服务器进行本地文件的存储

3 篇文章 0 订阅
2 篇文章 0 订阅

准备工作

yml配置

# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/DataBaseName?useUnicode=true&useSSL=false&characterEncoding=utf8
  servlet:
    multipart:
      enabled: true #是否启用http上传处理
      max-request-size: 100MB #最大请求文件的大小
      max-file-size: 20MB #设置单个文件最大长度
#file-size-threshold: 20MB #当文件达到多少时进行磁盘写入

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false # 数据库下划线自动转驼峰标示关闭

localhost: http://192.168.123.146:9090

swiperImage: ${localhost}/FTP/swiper/
FTP: ${localhost}/FTP/image/
ParcelImageAddress: ${localhost}/FTP/ParcelImage/
HeadImageAddress: ${localhost}/FTP/HeadImage/

SaveSwiperImageAddress: static/FTP/Swiper/
SaveParcelImageAddress: static/FTP/ParcelImage/
SaveHeadImageAddress: static/FTP/HeadImage/

其实要进行文件的本地的存储就是得在src下面的任何一个类里面就可以直接获取到resources里面的文件夹,那么你就可以进行操作了重点的代码如下:

  String resources = SaveFile.class.getResource("/").getPath().toString();
//SaveFile这个是你保存文件的一个类

文件保存类的主类

package com.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * @author:DUOLUONIANDAI
 * @DATA:2023/3/29 19:29
 * @Title:文件进行保存操作
 */
@Repository
public class SaveFile {

    @Value("${SaveParcelImageAddress}")
    String parcelImageAddress;

    @Value("${SaveHeadImageAddress}")
    String headImageAddress;

    @Value("${SaveSwiperImageAddress}")
    String swiperImageAddress;

    public boolean saveFilePlay(MultipartFile uploadFile, int play) {

        try {
            //
            String resources = SaveFile.class.getResource("/").getPath().toString();

            switch (play) {
                case 1:
                    resources += swiperImageAddress;//保存swiper
                    break;
                case 2:
                    resources += headImageAddress;//保存头像
                    break;
                case 3:
                    resources += parcelImageAddress;//保存包裹图片
                    break;
                default:{
                    return false;
                }
            }
            //保证数据的文件夹存在
            File file1 = new File(resources);
            System.out.println(resources);
            if(!file1.exists()){
                file1.mkdirs();
            }

            //保存后后缀名
            String originalFilename = uploadFile.getOriginalFilename();
            originalFilename = originalFilename.substring(originalFilename.lastIndexOf('.'));

            //生成id名
            UUID uuid = UUID.randomUUID();
            String name = uuid.toString() + originalFilename;
            System.out.println(name);

            //拼接路径
            String tfile = resources + name;
            System.out.println(tfile);

            //获取文件流
            InputStream inputStream = uploadFile.getInputStream();

             file1 = new File(tfile);

             //确保文件没有重复并创建容器
            while (file1.exists()) {
                uuid = UUID.randomUUID();
                name = uuid.toString() + originalFilename;
                System.out.println(name);

                tfile = resources + name;
                file1=new File(tfile);
            }
            file1.createNewFile();

            //遍历写入所有的数据
            FileOutputStream fileOutputStream = new FileOutputStream(file1);

            byte[] bytes = new byte[1000000000];
            int i;
            while ((i = inputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, i);
            }

            inputStream.close();
            fileOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

堕落年代

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值