zoukankan      html  css  js  c++  java
  • SpringBoot+El-upload实现上传文件到通用上传接口并返回文件全路径(若依前后端分离版源码分析)

    场景

    SpringBoot+ElementUI实现通用文件下载请求(全流程图文详细教程):

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/107983093

    在使用了通用下载接口下载一些文件后,有些业务比如上传头像,富文本上传照片,此类业务都需要将

    某些文件上传到服务器上,并返回该文件的全路径,给具体的业务前端返回,进行回显数据或下载文件用。

    同时为了后期文件数量过多,最好是按照时间在服务器上新建不用的文件夹,并且给上传的文件的

    文件名使用当前时间的时间戳进行重命名,避免文件重名。

     

    注:

    博客:
    https://blog.csdn.net/badao_liumang_qizhi
    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    实现

    首先在页面添加一个el-upload控件

        <el-upload
          class="quill-img"
          :action="uploadImgUrl"
          name="file"
          :headers="headers"
          :show-file-list="false"
          :on-success="quillImgSuccess"
          :on-error="uploadError"
          :before-upload="quillImgBefore"
          accept='.jpg,.jpeg,.png,.gif'
        ></el-upload>

    这里添加了一些样式,具体的样式代码

    .quill-img {
      display: none;
    }

    目的是先让其隐藏,只有在某些操作下才会触发上传的选择框。

    那么怎样触发这文件上传组件的点击事件,可以在需要弹出文件上传框的业务方法中,比如上传按钮的点击事件中

    // 触发input框选择图片文件
    document.querySelector(".quill-img input").click();

    action对应的是文件上传的url

    需要在属性中提前声明

      data() {
        return {
          uploadImgUrl: "",

    name是名称属性

    headers设置请求头携带token

    请求头里携带token,请求头的设置参考如下

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108345222

    show-file-list="false"设置不显示文件列表

    再设置它的一些事件

          :on-success="quillImgSuccess"
          :on-error="uploadError"
          :before-upload="quillImgBefore"

    对上传前,上传成功和失败的事件进行重写

        // 图片上传前
        quillImgBefore(file) {
          let fileType = file.type;
       if(fileType === 'image/jpeg' || fileType === 'image/png'){
        return true;
       }else {
        this.$message.error('请插入图片类型文件(jpg/jpeg/png)');
        return false;
       }
        },
    
        quillImgSuccess(res, file) {
          // res为图片服务器返回的数据
          // 获取富文本组件实例
          let quill = this.$refs.quillEditor.quill;
          // 如果上传成功
          if (res.code == 200) {
            // 获取光标所在位置
            let length = quill.getSelection().index;
            // 插入图片  res.url为服务器返回的图片地址
            quill.insertEmbed(length, "image", res.url);
            // 调整光标到最后
            quill.setSelection(length + 1);
          } else {
            this.$message.error("图片插入失败");
          }
        },
        // 富文本图片上传失败
        uploadError() {
          // loading动画消失
          this.$message.error("图片插入失败");
        }

    注意这里的图片上传成功是事件中,图片上传对应的后台SpringBoot接口 

        @PostMapping("/common/upload")
        public AjaxResult uploadFile(MultipartFile file) throws Exception {
            try {
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                ajax.put("url", url);
                return ajax;
            } catch (Exception e) {
                return AjaxResult.error(e.getMessage());
            }
        }

    在此接口中

    String filePath = RuoYiConfig.getUploadPath();

    是在application.yml中获取配置的文件上传的路径

    在application.yml中配置文件上传的路径

    ruoyi:
      # 名称
      name: RuoYi
      # 版本
      version: 2.3.0
      # 版权年份
      copyrightYear: 2019
      # 实例演示开关
      demoEnabled: true
      # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
      profile: D:/ruoyi/uploadPath

     然后就是使用配置类来获取这个配置的profile属性。

    首先在SpringBoot项目目录下新建config目录,然后新建配置类RuoYiConfig,名字随意

    然后在配置类上添加注解

    @Component
    @ConfigurationProperties(prefix = "ruoyi")
    public class RuoYiConfig
    注意这里的prefix属性值与上面配置文件的根元素一致

    然后配置类中的属性与配置文件根节点下的名称一致 ,配置类完整代码

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * 读取项目相关配置
     *
     */
    @Component
    @ConfigurationProperties(prefix = "ruoyi")
    public class RuoYiConfig
    {
        /** 项目名称 */
        private String name;
    
        /** 版本 */
        private String version;
    
        /** 版权年份 */
        private String copyrightYear;
    
        /** 实例演示开关 */
        private boolean demoEnabled;
    
        /** 上传路径 */
        private static String profile;
    
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public String getVersion()
        {
            return version;
        }
    
        public void setVersion(String version)
        {
            this.version = version;
        }
    
        public String getCopyrightYear()
        {
            return copyrightYear;
        }
    
        public void setCopyrightYear(String copyrightYear)
        {
            this.copyrightYear = copyrightYear;
        }
    
        public boolean isDemoEnabled()
        {
            return demoEnabled;
        }
    
        public void setDemoEnabled(boolean demoEnabled)
        {
            this.demoEnabled = demoEnabled;
        }
    
        public static String getProfile()
        {
            return profile;
        }
    
        public void setProfile(String profile)
        {
            RuoYiConfig.profile = profile;
        }
    
        /**
         * 获取上传路径
         */
        public static String getUploadPath()
        {
            return getProfile() + "/upload";
        }
    }

    这里的配置类的

    private static String profile;

    就能获取到application.yml中配置的profile的属性值了。

    为了或此属性值更加便捷,又新增了一个静态方法

        public static String getUploadPath()
        {
            return getProfile() + "/upload";
        }

    这样就能通过类直接调用方法。

    然后还拼接了一层目录。这样通过

    RuoYiConfig.getUploadPath();

    获取的路径就是

    D:/ruoyi/uploadPath/upload

    在获取了文件上传的路径之后调用了文件上传工具类的upload方法

        /**
         * 根据文件路径上传
         *
         * @param baseDir 相对应用的基目录
         * @param file 上传的文件
         * @return 文件名称
         * @throws IOException
         */
        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);
            }
        }

    此方法又追加了一个参数MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION

    此参数作用为限制文件上传的类型。

        public static final String[] DEFAULT_ALLOWED_EXTENSION = {
                // 图片
                "bmp", "gif", "jpg", "jpeg", "png",
                // word excel powerpoint
                "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
                // 压缩文件
                "rar", "zip", "gz", "bz2",
                // pdf
                "pdf" };

    然后在调用的方法

    upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);

        /**
         * 文件上传
         *
         * @param baseDir 相对应用的基目录
         * @param file 上传的文件
         * @param extension 上传文件类型
         * @return 返回上传成功的文件名
         * @throws FileSizeLimitExceededException 如果超出最大大小
         * @throws FileNameLengthLimitExceededException 文件名太长
         * @throws IOException 比如读写文件出错时
         * @throws InvalidExtensionException 文件校验异常
         */
        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;
        }

    首先校验文件名的长度是否超出指定的长度

            int fileNamelength = file.getOriginalFilename().length();
            if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
            {
                throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
            }

    默认的最大长度为100

        /**
         * 默认的文件名最大长度 100
         */
        public static final int DEFAULT_FILE_NAME_LENGTH = 100;

    如果超出最大长度则抛出自定义异常“文件名称超长限制异常”

    public class FileNameLengthLimitExceededException extends FileException
    {
        private static final long serialVersionUID = 1L;
    
        public FileNameLengthLimitExceededException(int defaultFileNameLength)
        {
            super("upload.filename.exceed.length", new Object[] { defaultFileNameLength });
        }
    }

    那么后面的代码则不会再执行。

    然后对文件大小进行校验

    assertAllowed(file, allowedExtension);

    此方法的实现为

        /**
         * 文件大小校验
         *
         * @param file 上传的文件
         * @return
         * @throws FileSizeLimitExceededException 如果超出最大大小
         * @throws InvalidExtensionException
         */
        public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
                throws FileSizeLimitExceededException, InvalidExtensionException
        {
            long size = file.getSize();
            if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
            {
                throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
            }
    
            String fileName = file.getOriginalFilename();
            String extension = getExtension(file);
            if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
            {
                if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
                            fileName);
                }
                else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
                            fileName);
                }
                else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
                            fileName);
                }
                else
                {
                    throw new InvalidExtensionException(allowedExtension, extension, fileName);
                }
            }
    
        }

    默认大小为50M

        /**
         * 默认大小 50M
         */
        public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;

    如果超出此大小则抛出自定义异常“文件大小超出限制异常类”

    /**
     * 文件大小限制异常类
     * 
     */
    public class FileSizeLimitExceededException extends FileException
    {
        private static final long serialVersionUID = 1L;
    
        public FileSizeLimitExceededException(long defaultMaxSize)
        {
            super("upload.exceed.maxSize", new Object[] { defaultMaxSize });
        }
    }

    那么后面的代码也不会再被执行

    然后就是获取文件的后缀

    String fileName = file.getOriginalFilename();
    String extension = getExtension(file);

    获取文件后缀名的方法

        /**
         * 获取文件名的后缀
         *
         * @param file 表单文件
         * @return 后缀名
         */
        public static final String getExtension(MultipartFile file)
        {
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            if (StringUtils.isEmpty(extension))
            {
                extension = MimeTypeUtils.getExtension(file.getContentType());
            }
            return extension;
        }

    这其中又用到

        public static String getExtension(String prefix)
        {
            switch (prefix)
            {
                case IMAGE_PNG:
                    return "png";
                case IMAGE_JPG:
                    return "jpg";
                case IMAGE_JPEG:
                    return "jpeg";
                case IMAGE_BMP:
                    return "bmp";
                case IMAGE_GIF:
                    return "gif";
                default:
                    return "";
            }
        }

    获取到后缀名之后就拿允许的扩展名与上传做对比

        /**
         * 判断MIME类型是否是允许的MIME类型
         *
         * @param extension
         * @param allowedExtension
         * @return
         */
        public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
        {
            for (String str : allowedExtension)
            {
                if (str.equalsIgnoreCase(extension))
                {
                    return true;
                }
            }
            return false;
        }

    后面就是一系列的不同后缀名类型的自定义异常的抛出。

    一旦有任何一种异常抛出则下面的异常就不会再被执行。

            if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
            {
                if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
                            fileName);
                }
                else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
                            fileName);
                }
                else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
                            fileName);
                }
                else
                {
                    throw new InvalidExtensionException(allowedExtension, extension, fileName);
                }
            }

    附媒体类型工具类MimeTypeUtils代码

    package com.ruoyi.common.utils.file;
    
    /**
     * 媒体类型工具类
     *
     * @author ruoyi
     */
    public class MimeTypeUtils
    {
        public static final String IMAGE_PNG = "image/png";
    
        public static final String IMAGE_JPG = "image/jpg";
    
        public static final String IMAGE_JPEG = "image/jpeg";
    
        public static final String IMAGE_BMP = "image/bmp";
    
        public static final String IMAGE_GIF = "image/gif";
       
        public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" };
    
        public static final String[] FLASH_EXTENSION = { "swf", "flv" };
    
        public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
                "asf", "rm", "rmvb" };
    
        public static final String[] DEFAULT_ALLOWED_EXTENSION = {
                // 图片
                "bmp", "gif", "jpg", "jpeg", "png",
                // word excel powerpoint
                "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
                // 压缩文件
                "rar", "zip", "gz", "bz2",
                // pdf
                "pdf" };
    
        public static String getExtension(String prefix)
        {
            switch (prefix)
            {
                case IMAGE_PNG:
                    return "png";
                case IMAGE_JPG:
                    return "jpg";
                case IMAGE_JPEG:
                    return "jpeg";
                case IMAGE_BMP:
                    return "bmp";
                case IMAGE_GIF:
                    return "gif";
                default:
                    return "";
            }
        }
    }
    最终抛出的无效扩展名异常InvalidExtensionException
    package com.ruoyi.common.exception.file;
    
    import java.util.Arrays;
    import org.apache.commons.fileupload.FileUploadException;
    
    /**
     * 文件上传 误异常类
     *
     * @author ruoyi
     */
    public class InvalidExtensionException extends FileUploadException
    {
        private static final long serialVersionUID = 1L;
    
        private String[] allowedExtension;
        private String extension;
        private String filename;
    
        public InvalidExtensionException(String[] allowedExtension, String extension, String filename)
        {
            super("filename : [" + filename + "], extension : [" + extension + "], allowed extension : [" + Arrays.toString(allowedExtension) + "]");
            this.allowedExtension = allowedExtension;
            this.extension = extension;
            this.filename = filename;
        }
    
        public String[] getAllowedExtension()
        {
            return allowedExtension;
        }
    
        public String getExtension()
        {
            return extension;
        }
    
        public String getFilename()
        {
            return filename;
        }
    
        public static class InvalidImageExtensionException extends InvalidExtensionException
        {
            private static final long serialVersionUID = 1L;
    
            public InvalidImageExtensionException(String[] allowedExtension, String extension, String filename)
            {
                super(allowedExtension, extension, filename);
            }
        }
    
        public static class InvalidFlashExtensionException extends InvalidExtensionException
        {
            private static final long serialVersionUID = 1L;
    
            public InvalidFlashExtensionException(String[] allowedExtension, String extension, String filename)
            {
                super(allowedExtension, extension, filename);
            }
        }
    
        public static class InvalidMediaExtensionException extends InvalidExtensionException
        {
            private static final long serialVersionUID = 1L;
    
            public InvalidMediaExtensionException(String[] allowedExtension, String extension, String filename)
            {
                super(allowedExtension, extension, filename);
            }
        }
    }

    再回到upload方法

    验证文件大小没有问题后没有任何异常抛出则

    String fileName = extractFilename(file);

    对文件名进行编码

        /**
         * 编码文件名
         */
        public static final String extractFilename(MultipartFile file)
        {
            String fileName = file.getOriginalFilename();
            String extension = getExtension(file);
            fileName = DateUtils.datePath() + "/" + encodingFilename(fileName) + "." + extension;
            return fileName;
        }

    在编码文件名的方法中生成日期路径的方法

        /**
         * 日期路径 即年/月/日 如2018/08/08
         */
        public static final String datePath() {
            Date now = new Date();
            return DateFormatUtils.format(now, "yyyy/MM/dd");
        }

    其中DateFormatUtils是org.apache.commons.lang3.time包中的。

    然后对文件名进行编码防止重复

        /**
         * 编码文件名
         */
        private static final String encodingFilename(String fileName)
        {
            fileName = fileName.replace("_", " ");
            fileName = Md5Utils.hash(fileName + System.nanoTime() + counter++);
            return fileName;
        }

    编码方式采用MD5工具类的hash方法加上当前时间的毫秒与累加的计数器。

    MD5工具类

    package com.ruoyi.common.utils.security;
    
    import java.security.MessageDigest;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * Md5加密方法
     *
     * @author ruoyi
     */
    public class Md5Utils
    {
        private static final Logger log = LoggerFactory.getLogger(Md5Utils.class);
    
        private static byte[] md5(String s)
        {
            MessageDigest algorithm;
            try
            {
                algorithm = MessageDigest.getInstance("MD5");
                algorithm.reset();
                algorithm.update(s.getBytes("UTF-8"));
                byte[] messageDigest = algorithm.digest();
                return messageDigest;
            }
            catch (Exception e)
            {
                log.error("MD5 Error...", e);
            }
            return null;
        }
    
        private static final String toHex(byte hash[])
        {
            if (hash == null)
            {
                return null;
            }
            StringBuffer buf = new StringBuffer(hash.length * 2);
            int i;
    
            for (i = 0; i < hash.length; i++)
            {
                if ((hash[i] & 0xff) < 0x10)
                {
                    buf.append("0");
                }
                buf.append(Long.toString(hash[i] & 0xff, 16));
            }
            return buf.toString();
        }
    
        public static String hash(String s)
        {
            try
            {
                return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8");
            }
            catch (Exception e)
            {
                log.error("not supported charset...{}", e);
                return s;
            }
        }
    }

    这样经过编码后的文件路径就是类似下面的路径

    D: uoyiuploadPathupload2020926bd175725b91ec9b9f3bda0d01849479.jpg

     

    编码完文件名后就要在服务器上生成相应的目录

        private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
        {
            File desc = new File(uploadDir + File.separator + fileName);
    
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
            if (!desc.exists())
            {
                desc.createNewFile();
            }
            return desc;
        }

    然后将上传的文件存储到相应的目录

    file.transferTo(desc);

    此时再获取资源映射路径,返回给前端用

    String pathFileName = getPathFileName(baseDir, fileName);

    此方法的实现为

        private static final String getPathFileName(String uploadDir, String fileName) throws IOException
        {
            int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
            String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
            String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
            return pathFileName;
        }

    用到了截取字符串的方法 

       /**
         * 截取字符串
         *
         * @param str 字符串
         * @param start 开始
         * @return 结果
         */
        public static String substring(final String str, int start)
        {
            if (str == null)
            {
                return NULLSTR;
            }
    
            if (start < 0)
            {
                start = str.length() + start;
            }
    
            if (start < 0)
            {
                start = 0;
            }
            if (start > str.length())
            {
                return NULLSTR;
            }
    
            return str.substring(start);
        }

    其中

        /** 空字符串 */
        private static final String NULLSTR = "";

    截取配置的资源存放路径的后面的相对路径

    然后拼接上项目中配置的静态资源映射的路径的前缀

        /**
         * 资源映射路径 前缀
         */
        public static final String RESOURCE_PREFIX = "/profile";

    静态资源的映射配置类ResourcesConfig代码

    package com.ruoyi.framework.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import com.ruoyi.common.constant.Constants;
    import com.ruoyi.framework.interceptor.RepeatSubmitInterceptor;
    
    /**
     * 通用配置
     *
     * @author ruoyi
     */
    @Configuration
    public class ResourcesConfig implements WebMvcConfigurer
    {
        @Autowired
        private RepeatSubmitInterceptor repeatSubmitInterceptor;
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry)
        {
            /** 本地文件上传路径 */
            registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");
    
            /** swagger配置 */
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    
        /**
         * 自定义拦截规则
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry)
        {
            registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
        }
    
     }

    这样upload方法返回的pathFileName

    就是在服务器上静态资源映射好的相对路径。

    附FileUploadUtils完整代码

    package com.ruoyi.common.utils.file;
    
    import java.io.File;
    import java.io.IOException;
    import org.apache.commons.io.FilenameUtils;
    import org.springframework.web.multipart.MultipartFile;
    import com.ruoyi.common.constant.Constants;
    import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
    import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
    import com.ruoyi.common.exception.file.InvalidExtensionException;
    import com.ruoyi.common.utils.DateUtils;
    import com.ruoyi.common.utils.StringUtils;
    import com.ruoyi.common.utils.security.Md5Utils;
    import com.ruoyi.framework.config.RuoYiConfig;
    
    /**
     * 文件上传工具类
     *
     * @author ruoyi
     */
    public class FileUploadUtils
    {
        /**
         * 默认大小 50M
         */
        public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
    
        /**
         * 默认的文件名最大长度 100
         */
        public static final int DEFAULT_FILE_NAME_LENGTH = 100;
    
        /**
         * 默认上传的地址
         */
        private static String defaultBaseDir = RuoYiConfig.getProfile();
    
        private static int counter = 0;
    
        public static void setDefaultBaseDir(String defaultBaseDir)
        {
            FileUploadUtils.defaultBaseDir = defaultBaseDir;
        }
    
        public static String getDefaultBaseDir()
        {
            return defaultBaseDir;
        }
    
        /**
         * 以默认配置进行文件上传
         *
         * @param file 上传的文件
         * @return 文件名称
         * @throws Exception
         */
        public static final String upload(MultipartFile file) throws IOException
        {
            try
            {
                return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
            }
            catch (Exception e)
            {
                throw new IOException(e.getMessage(), e);
            }
        }
    
        /**
         * 根据文件路径上传
         *
         * @param baseDir 相对应用的基目录
         * @param file 上传的文件
         * @return 文件名称
         * @throws IOException
         */
        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);
            }
        }
    
        /**
         * 文件上传
         *
         * @param baseDir 相对应用的基目录
         * @param file 上传的文件
         * @param extension 上传文件类型
         * @return 返回上传成功的文件名
         * @throws FileSizeLimitExceededException 如果超出最大大小
         * @throws FileNameLengthLimitExceededException 文件名太长
         * @throws IOException 比如读写文件出错时
         * @throws InvalidExtensionException 文件校验异常
         */
        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;
        }
    
        /**
         * 编码文件名
         */
        public static final String extractFilename(MultipartFile file)
        {
            String fileName = file.getOriginalFilename();
            String extension = getExtension(file);
            fileName = DateUtils.datePath() + "/" + encodingFilename(fileName) + "." + extension;
            return fileName;
        }
    
        private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
        {
            File desc = new File(uploadDir + File.separator + fileName);
    
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
            if (!desc.exists())
            {
                desc.createNewFile();
            }
            return desc;
        }
    
        private static final String getPathFileName(String uploadDir, String fileName) throws IOException
        {
            int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
            String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
            String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
            return pathFileName;
        }
    
        /**
         * 编码文件名
         */
        private static final String encodingFilename(String fileName)
        {
            fileName = fileName.replace("_", " ");
            fileName = Md5Utils.hash(fileName + System.nanoTime() + counter++);
            return fileName;
        }
    
        /**
         * 文件大小校验
         *
         * @param file 上传的文件
         * @return
         * @throws FileSizeLimitExceededException 如果超出最大大小
         * @throws InvalidExtensionException
         */
        public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
                throws FileSizeLimitExceededException, InvalidExtensionException
        {
            long size = file.getSize();
            if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
            {
                throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
            }
    
            String fileName = file.getOriginalFilename();
            String extension = getExtension(file);
            if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
            {
                if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
                            fileName);
                }
                else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
                            fileName);
                }
                else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
                {
                    throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
                            fileName);
                }
                else
                {
                    throw new InvalidExtensionException(allowedExtension, extension, fileName);
                }
            }
    
        }
    
        /**
         * 判断MIME类型是否是允许的MIME类型
         *
         * @param extension
         * @param allowedExtension
         * @return
         */
        public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
        {
            for (String str : allowedExtension)
            {
                if (str.equalsIgnoreCase(extension))
                {
                    return true;
                }
            }
            return false;
        }
    
        /**
         * 获取文件名的后缀
         *
         * @param file 表单文件
         * @return 后缀名
         */
        public static final String getExtension(MultipartFile file)
        {
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            if (StringUtils.isEmpty(extension))
            {
                extension = MimeTypeUtils.getExtension(file.getContentType());
            }
            return extension;
        }
    
     }

    再回到接口Controller,还需要拼接上当前服务器的域名和ip

     String url = serverConfig.getUrl() + fileName;

    调用了服务器相关配置的获取完整的请求路径的方法

    package com.ruoyi.framework.config;
    
    import javax.servlet.http.HttpServletRequest;
    import org.springframework.stereotype.Component;
    import com.ruoyi.common.utils.ServletUtils;
    
    /**
     * 服务相关配置
     *
     * @author ruoyi
     */
    @Component
    public class ServerConfig
    {
        /**
         * 获取完整的请求路径,包括:域名,端口,上下文访问路径
         *
         * @return 服务地址
         */
        public String getUrl()
        {
            HttpServletRequest request = ServletUtils.getRequest();
            return getDomain(request);
        }
    
        public static String getDomain(HttpServletRequest request)
        {
            StringBuffer url = request.getRequestURL();
            String contextPath = request.getServletContext().getContextPath();
            return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
        }
    
    }

    在获取请求对象时调用了客户端工具类的ServletUtils.getRequest();

       /**
         * 获取request
         */
        public static HttpServletRequest getRequest()
        {
            return getRequestAttributes().getRequest();
        }

    其中用到了

        public static ServletRequestAttributes getRequestAttributes()
        {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            return (ServletRequestAttributes) attributes;
        } 

    其中RequestContextHolder在

    org.springframework.web.context.request下

    最终后台接口返回的url是一个在服务器上绝对路径的图片地址,比如

    http://localhost:8080/profile/upload/2020/09/02/e070fd1a26dca6c00acf6db1bc467905.png

    接口返回时AjaxResult是自定义的响应类

    package com.ruoyi.framework.web.domain;
    
    import java.util.HashMap;
    import com.ruoyi.common.constant.HttpStatus;
    import com.ruoyi.common.utils.StringUtils;
    
    /**
     * 操作消息提醒
     *
     * @author ruoyi
     */
    public class AjaxResult extends HashMap<String, Object>
    {
        private static final long serialVersionUID = 1L;
    
        /** 状态码 */
        public static final String CODE_TAG = "code";
    
        /** 返回内容 */
        public static final String MSG_TAG = "msg";
    
        /** 数据对象 */
        public static final String DATA_TAG = "data";
    
        /**
         * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
         */
        public AjaxResult()
        {
        }
    
        /**
         * 初始化一个新创建的 AjaxResult 对象
         *
         * @param code 状态码
         * @param msg 返回内容
         */
        public AjaxResult(int code, String msg)
        {
            super.put(CODE_TAG, code);
            super.put(MSG_TAG, msg);
        }
    
        /**
         * 初始化一个新创建的 AjaxResult 对象
         *
         * @param code 状态码
         * @param msg 返回内容
         * @param data 数据对象
         */
        public AjaxResult(int code, String msg, Object data)
        {
            super.put(CODE_TAG, code);
            super.put(MSG_TAG, msg);
            if (StringUtils.isNotNull(data))
            {
                super.put(DATA_TAG, data);
            }
        }
    
        /**
         * 返回成功消息
         *
         * @return 成功消息
         */
        public static AjaxResult success()
        {
            return AjaxResult.success("操作成功");
        }
    
        /**
         * 返回成功数据
         *
         * @return 成功消息
         */
        public static AjaxResult success(Object data)
        {
            return AjaxResult.success("操作成功", data);
        }
    
        /**
         * 返回成功消息
         *
         * @param msg 返回内容
         * @return 成功消息
         */
        public static AjaxResult success(String msg)
        {
            return AjaxResult.success(msg, null);
        }
    
        /**
         * 返回成功消息
         *
         * @param msg 返回内容
         * @param data 数据对象
         * @return 成功消息
         */
        public static AjaxResult success(String msg, Object data)
        {
            return new AjaxResult(HttpStatus.SUCCESS, msg, data);
        }
    
        /**
         * 返回错误消息
         *
         * @return
         */
        public static AjaxResult error()
        {
            return AjaxResult.error("操作失败");
        }
    
        /**
         * 返回错误消息
         *
         * @param msg 返回内容
         * @return 警告消息
         */
        public static AjaxResult error(String msg)
        {
            return AjaxResult.error(msg, null);
        }
    
        /**
         * 返回错误消息
         *
         * @param msg 返回内容
         * @param data 数据对象
         * @return 警告消息
         */
        public static AjaxResult error(String msg, Object data)
        {
            return new AjaxResult(HttpStatus.ERROR, msg, data);
        }
    
        /**
         * 返回错误消息
         *
         * @param code 状态码
         * @param msg 返回内容
         * @return 警告消息
         */
        public static AjaxResult error(int code, String msg)
        {
            return new AjaxResult(code, msg, null);
        }
    }

    这样讲就能实现通用文件上传接口并返回在服务器上能访问到的路径。

    博客园: https://www.cnblogs.com/badaoliumangqizhi/ 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。
  • 相关阅读:
    无法识别的属性“targetFramework”。请注意属性名称区分大写和小写。错误解决的方法
    OpenGL 4 : 一个漂亮的心 For you, My Love
    request.getParameterValues与request.getParameter的差别
    Mac下搭建quick cocos2d-x编译环境
    【Github教程】史上最全github用法:github入门到精通
    OSI七层模型具体解释
    Android Service 服务(一)—— Service
    几种更新(Update语句)查询的方法
    epoll使用具体解释(精髓)
    SSL连接建立过程分析(1)
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/13607774.html
Copyright © 2011-2022 走看看