zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Springboot文件上传_maven上传jar包到远程仓库

SpringBootMavenjar文件上传 远程 仓库 包到
2023-06-13 09:13:03 时间

springboot文件上传机制:

注意,存储到数据库中的只是路径名 1.访问路径

2. 上传完成后返回访问文件地址

3. 我们只需要访问返回的地址就可以访问到图片

4. yaml配置文件(localpath是实际存储的地址)

5. 添加配置类,进行访问地址和存储地址映射

    @Value("${file.upload.suffixPath}")
    private String uploadSuffixPath;
    @Value("${file.upload.localPath}")
    private String uploadLocalPath;
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){ 
   
        AppFileUtils.uploadSuffixPath = uploadSuffixPath;
        AppFileUtils.uploadLocalPath = uploadLocalPath;
        registry.addResourceHandler(uploadSuffixPath+"/**").addResourceLocations("file:"+uploadLocalPath);
    }
  1. 实际上传文件代码
package com.yglh.utils;

import org.aspectj.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@Component
public class AppFileUtils { 
   

    private final static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /** * 文件上传路径前缀 */
    public static String uploadSuffixPath;
    /** * 本地磁盘目录 */
    public static String uploadLocalPath;

    /** * date format yyyyMMdd */
    public static final String PATTERN_yyyyMMdd = "yyyyMMdd";
    /** * date format yyyyMMddHHmmssSSS */
    public static final String PATTERN_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";


    /** * @Description: 单文件上传到本地磁盘 * @param multipartFile * @return: java.lang.String * @Author: lxt * @Date: 2021/3/29 10:30 */
    public static String uploadFile(MultipartFile multipartFile){ 
   
        if(multipartFile == null){ 
   
            return null;
        }
        //产生文件名称
        String fileName = getUploadFileName(multipartFile.getOriginalFilename());
        //产生文件目录
        String dateDir = format(null,PATTERN_yyyyMMdd);
        File destFileDir = new File(uploadLocalPath + File.separator + dateDir);
        //因为目录每天凌晨更新所以要判断是否存在,如果不存在就创建新的文件
        if(!destFileDir.exists()){ 
   
            destFileDir.mkdirs();
        }
        try { 
   
        	//把上传文件到目的目录中去
            File destFile = new File(destFileDir.getAbsoluteFile()+File.separator+fileName);
            multipartFile.transferTo(destFile);
            logger.info("文件【"+multipartFile.getOriginalFilename()+"】上传成功");
            return uploadSuffixPath + "/" + dateDir+"/"+fileName;
        } catch (IOException e) { 
   
            logger.error("文件上传异常:"+multipartFile.getOriginalFilename(),e);
            return null;
        }
    }


    /** * @Description: 获取上传后的文件相对路径 --数据库存储该路径 * @param fileName * @return: java.lang.String * @Author: lxt * @Date: 2021/3/29 10:30 */
    public static String getUploadFileName(String fileName){ 
   
        return new StringBuilder()
                .append(format(null, PATTERN_yyyyMMddHHmmssSSS))
                .append("_").append(getRandomStrByNum(6))
                .append(getExtension(fileName))
                .toString();
    }

    /** * 格式化时间 * @param localDateTime * @param pattern 格式 * @return */
    public static String format(LocalDateTime localDateTime, String pattern){ 
   
        //默认取当前时间
        localDateTime = localDateTime == null ? LocalDateTime.now() : localDateTime;
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String CHAR_STR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    /** * @Title: getRandomStrByNum * @Description: 获取不同位数的随机字符串 * @Author: lxt * @param: factor * @return: java.lang.String * @throws: */
    public static String getRandomStrByNum(int factor) { 
   
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < factor; i++) { 
   
            sb.append(CHAR_STR.charAt(random.nextInt(36)));
        }
        return sb.toString();
    }
    /** * @Description: 获取扩展名 * @param fileName * @return: java.lang.String * @Author: lxt */
    public static String getExtension(String fileName){ 
   
        return fileName.substring(fileName.lastIndexOf("."));
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168899.html原文链接:https://javaforall.cn