Springboot 工程中快速判断web应用服务器类型

文章目录

一、核心源码

java 复制代码
    boolean isTomcat;
    
    boolean isJetty;
    
    @Autowired
    ApplicationContext applicationContext;
    
    @PostConstruct
    private void init()
    {
        isTomcat = Stream.of(applicationContext.getBeanDefinitionNames()).anyMatch(name -> StringUtils.containsIgnoreCase(name, "EmbeddedTomcat"));
        isJetty = Stream.of(applicationContext.getBeanDefinitionNames()).anyMatch(name -> StringUtils.containsIgnoreCase(name, "EmbeddedJetty"));
        log.info("#### isTomcat: {}", isTomcat);
        log.info("#### isJetty: {}", isJetty);
    }

二、典型应用

java 复制代码
import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;

import javax.annotation.PostConstruct;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.fly.demo.common.JsonResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "文件上传(simple)")
@RestController
@RequestMapping("/simple/file")
public class SimpleFileController
{
    boolean isTomcat;
    
    boolean isJetty;
    
    @Autowired
    ApplicationContext applicationContext;
    
    @PostConstruct
    private void init()
    {
        isTomcat = Stream.of(applicationContext.getBeanDefinitionNames()).anyMatch(name -> StringUtils.containsIgnoreCase(name, "EmbeddedTomcat"));
        isJetty = Stream.of(applicationContext.getBeanDefinitionNames()).anyMatch(name -> StringUtils.containsIgnoreCase(name, "EmbeddedJetty"));
        log.info("#### isTomcat: {}", isTomcat);
        log.info("#### isJetty: {}", isJetty);
    }
    
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public JsonResult<?> upload(@RequestParam MultipartFile file)
        throws IOException
    {
        File rootDir = new File("upload");
        File dest;
        if (isTomcat)
        {
            if (RandomUtils.nextBoolean())
            {
                log.info("### transferTo");
                dest = new File(rootDir.getCanonicalPath() + File.separator + file.getOriginalFilename());
                file.transferTo(dest);
            }
            else
            {
                log.info("### copyInputStreamToFile");
                dest = new File(rootDir, file.getOriginalFilename());
                FileUtils.copyInputStreamToFile(file.getInputStream(), dest);
            }
        }
        else
        {
            log.info("### copyInputStreamToFile");
            dest = new File(rootDir, file.getOriginalFilename());
            FileUtils.copyInputStreamToFile(file.getInputStream(), dest);
        } 
        return JsonResult.success(dest.getName());
    }
}
相关推荐
她的男孩几秒前
JAR 全对仍 checksum mismatch:Forge Admin 把 Flyway 从 filesystem 改成 classpath
java·数据库·spring boot
鬼手点金2 分钟前
如何选择模型
java·前端·人工智能·git·深度学习
古法老登瞎折腾8 分钟前
大数据智能运维实战(三):实现第一个运维 MCP 工具——查询节点与组件状态
java·大数据·运维·人工智能·spring ai·mcp
小蒜学长10 分钟前
基于数据挖掘的京东商城商品推荐系统设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端·推荐系统·个性化推荐
IT研究室24 分钟前
最新计算机毕业设计选题推荐-基于spring boot的医院体检管理系统-网站-文档指导-Java-springboot
java·spring boot·课程设计
长不大的杨瑞杰28 分钟前
把 Spring Boot 接口变成 AI 工具:OpenAPI、参数映射与异常语义化实战
人工智能·spring boot·后端
有同事要进步29 分钟前
Leecode第68题:x的平方根
java·数据结构·算法
摇滚侠31 分钟前
使用 VSCode 开发 Java 经验分享
java·经验分享·vscode
吹什么轩35 分钟前
linux系统复习:进程优先级与O(1)调度
java·linux·算法
RuoyiOffice36 分钟前
Vue3+TypeScript 企业管理平台统一UI:新增一个模块,为什么还要重新调样式?
spring boot·typescript·vue3·组件化开发·企业管理软件·ruoyi office·统一ui