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

Springboot文件上传_maven上传jar包到远程仓库springboot文件上传机制:1.访问路径2. 上传完成后返回访问文件地址3. 我们只需要访问返回的地址就可以访问到图片4. yaml配置文件(localpath是实际存储的地址)5. 添加配置类,进行访问地址和存储地址映射 @Value(“${file.upload.suffixPath}”) private String uploadSuffixPath; @Value(“${file.upload.localPath}”) private Strin

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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.net/168750.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • DOS分区表(Boot Sector引导扇区)[通俗易懂]

    DOS分区表(Boot Sector引导扇区)[通俗易懂]>>DOS分区体系的硬盘也叫MBR硬盘,0号扇区是主引导记录MBR,DOS分区体系的硬盘用分区表记录每个分区的类型起始位置和分区的大小。其中,分区表就在0号扇区内,所以0号扇区如果损坏,那么这个硬盘就不能正确识别分区。>>DOS分区的使用范围:windows系统,Linux系统以及基于IA32平台FreeDBS和OpenDBS等操作系统都使用DOS分区体系。&g…

    2022年10月23日
  • 基于深度学习的图像超分辨率重建技术的研究

    1超分辨率重建技术的研究背景与意义图像分辨率是一组用于评估图像中蕴含细节信息丰富程度的性能参数,包括时间分辨率、空间分辨率及色阶分辨率等,体现了成像系统实际所能反映物体细节信息的能力。相较于低分辨率图像,高分辨率图像通常包含更大的像素密度、更丰富的纹理细节及更高的可信赖度。但在实际上中,受采集设备与环境、网络传输介质与带宽、图像退化模型本身等诸多因素的约束,我们通常并不能直接得到具有边缘锐化、无成块模糊的理想高分辨率图像。提升图像分辨率的最直接的做法是对采集系统中的光学硬件进行改进,但这种做法.

    2022年4月9日
  • db4o数据库的基本操作

    db4o数据库的基本操作对db4o(面向对象的数据库)的基本操作: 数据库的链接:privatestaticObjectContainerdb; finalstaticStringDB4OFILENAME=System.getProperty("user.dir") +"/database.db4o"; static{ db=Db4oEmbedde…

    2022年7月21日
  • Linux下tomcat的安装与项目部署

    Linux下tomcat的安装与项目部署

    2021年9月16日
  • vs2012创建WCF应用程序

    vs2012创建WCF应用程序vs2012创建WCF应用程序

    2022年4月25日
  • poj -2632 Crashing Robots

    poj -2632 Crashing Robots

    2022年1月14日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号