栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot文件上传

SpringBoot文件上传

文章目录
  • SpringBoot文件上传
    • 文件上传的本质
    • 文件上传的细节点
    • 获取原文件名称
    • 获取新文件路径(名称)
    • 将远程文件存入本地
    • 修改上传文件类型
    • 案例

文件上传的本质

文件上传的流程,相当于是复制,是读取了要上传文件的二进制流,并把这个二进制流的信息复制给服务器指定路径的另一个文件

文件上传的细节点
  • 在服务器中需要有专门的目录来存放上传路径 ,在新的上传请求传来时,必应判断该上传路径是否存在,若不存在 ,则需要先创建该上传目录。

  • 在服务器中的文件,为了防止上传文件的名称重复(防止重复提交同一文件),从而产生冲突等问题,应用 UUID 来生成随机 ID 为新文件命名

  • 文件上传应当将上传至服务器的url地址保存在数据库中

获取原文件名称

在前端向后端发起上传请求时,传参中必然带有文件信息,此时在 Controller 层对应的接口中会接收到 MultipartFile 类型的 file

通过以下代码可获取原文件的信息:

//原文件名称
String OriginFileName = file.getOriginalFilename();
//原文件文件名最大长度
int fileNamelength = file.getOriginalFilename().length()
获取新文件路径(名称)
    public static final String extractFilename(MultipartFile file)
    {
        String fileName = file.getOriginalFilename();
        //获得文件名后缀
        String extension = getExtension(file);
        //拼接上时间
        fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
        return fileName;
    }

为什么要拼接上时间,是因为window类似mysql也有自己的索引规则,通过时间来查找,能减轻搜索压力

将远程文件存入本地

假设,存放新文件的变量名称为 newFile ,远程文件变量名称为 oldFile

则可通过以下代码将远程文件保存至本地文件中:

oldFile.transferTo(newFile);
修改上传文件类型
upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);

其中DEFAULT_ALLOWED_EXTENSION是上传时候默认携带的一个参数,存储了一个集合(文件类型)

可以发现这里面没有视频的类型,要想该上传可以通过视频类型的文件,可以在这个集合上加上视频对应的类型

案例

前端采取ElementUI上传的组件

这个项目上传是需要验证权限的,所以请求时候在请求头携带了一个认证的参数






后端

CommonController

这里处理上传后的文件名称和请求路径进行一个拼接

public class CommonController {
    @Autowired
    private ServerConfig serverConfig;
    
    @PostMapping("/upload")
    public ResultJson uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径(配置文件中的)
            String filePath = NiuaConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            //获取请求路径
            String url = serverConfig.getUrl() + fileName;//http://127.0.0.1:9527
            Map prams = new HashMap();
            prams.put("fileName", fileName);
            //profile/upload/2022/07/28/94ac7ece-3371-40fb-80d0-8f42231df2f7.jpg
            prams.put("url", url);
            //http://127.0.0.1:9527/profile/upload/2022/07/28/94ac7ece-3371-40fb-80d0-8f42231df2f7.jpg
            return ResultJson.ok(prams);
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.BAD_REQUEST, e.getMessage());
        }
    }
}

获取新文件名称

String fileName = FileUploadUtils.upload(filePath, file);

根据文件路径上传,这里携带了DEFAULT_ALLOWED_EXTENSION参数,表示默认文件上传的类型

 
    public static final String upload(String baseDir, MultipartFile file) throws IOException
    {
        try
        {
            return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        }
        catch (Exception e)
        {
            throw new IOException(e.getMessage(), e);
        }
    }

文件上传

    public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
            InvalidExtensionException
    {
        //文件大小检验
        int fileNamelength = file.getOriginalFilename().length();
        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
        {
            throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
        }
        //文件类型检验
        assertAllowed(file, allowedExtension);
        //获取拼接说时间的文件名称(编码新文件名)
        String fileName = extractFilename(file);
		//返回一个文件的绝对路径
        File desc = getAbsoluteFile(baseDir, fileName);
        //把远程文件存入本地
        file.transferTo(desc);
        String pathFileName = getPathFileName(baseDir, fileName);
        return pathFileName;//profile/upload/2022/07/28/94ac7ece-3371-40fb-80d0-8f42231df2f7.jpg
    }

返回的数据格式ResultJson.java

package tech.niua.common.model;

import lombok.Data;

import java.io.Serializable;


@Data
public class ResultJson implements Serializable{

    private static final long serialVersionUID = 783015033603078674L;
    private int code;
    private String msg;
    private T data;

    public static ResultJson ok() {
        return ok("");
    }

    public static ResultJson ok(Object o) {
        return new ResultJson(ResultCode.SUCCESS, o);
    }

    public static ResultJson failure(ResultCode code) {
        return failure(code, "");
    }

    public static ResultJson failure(ResultCode code, Object o) {
        return new ResultJson(code, o);
    }

    public ResultJson(ResultCode resultCode) {
        setResultCode(resultCode);
    }

    public ResultJson(ResultCode resultCode, T data) {
        setResultCode(resultCode);
        this.data = data;
    }

    public void setResultCode(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    @Override
    public String toString() {
        return "{" +
                ""code":" + code +
                ", "msg":"" + msg + '"' +
                ", "data":"" + data + '"'+
                '}';
    }
}

返回的数据格式ResultJson.java

package tech.niua.common.model;

import lombok.Data;

import java.io.Serializable;


@Data
public class ResultJson implements Serializable{

    private static final long serialVersionUID = 783015033603078674L;
    private int code;
    private String msg;
    private T data;

    public static ResultJson ok() {
        return ok("");
    }

    public static ResultJson ok(Object o) {
        return new ResultJson(ResultCode.SUCCESS, o);
    }

    public static ResultJson failure(ResultCode code) {
        return failure(code, "");
    }

    public static ResultJson failure(ResultCode code, Object o) {
        return new ResultJson(code, o);
    }

    public ResultJson(ResultCode resultCode) {
        setResultCode(resultCode);
    }

    public ResultJson(ResultCode resultCode, T data) {
        setResultCode(resultCode);
        this.data = data;
    }

    public void setResultCode(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    @Override
    public String toString() {
        return "{" +
                ""code":" + code +
                ", "msg":"" + msg + '"' +
                ", "data":"" + data + '"'+
                '}';
    }
}

返回的数据格式ResultJson.java

package tech.niua.common.model;

import lombok.Data;

import java.io.Serializable;


@Data
public class ResultJson implements Serializable{

    private static final long serialVersionUID = 783015033603078674L;
    private int code;
    private String msg;
    private T data;

    public static ResultJson ok() {
        return ok("");
    }

    public static ResultJson ok(Object o) {
        return new ResultJson(ResultCode.SUCCESS, o);
    }

    public static ResultJson failure(ResultCode code) {
        return failure(code, "");
    }

    public static ResultJson failure(ResultCode code, Object o) {
        return new ResultJson(code, o);
    }

    public ResultJson(ResultCode resultCode) {
        setResultCode(resultCode);
    }

    public ResultJson(ResultCode resultCode, T data) {
        setResultCode(resultCode);
        this.data = data;
    }

    public void setResultCode(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    @Override
    public String toString() {
        return "{" +
                ""code":" + code +
                ", "msg":"" + msg + '"' +
                ", "data":"" + data + '"'+
                '}';
    }
}

编码新文件名

通过UUID(避免重复)给文件编码新的文件名,并且按照指定的样式返回

    public static final String extractFilename(MultipartFile file)
    {
        String fileName = file.getOriginalFilename();
        //获得文件名后缀
        String extension = getExtension(file);
        //拼接上时间
        fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
        return fileName;
    }

上传成功效果图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1031280.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号