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());
    }
}
相关推荐
EatFan5 小时前
Spring Boot 4 落地观察:从 yudao-cloud、matecloud、JPower 看国产脚手架的升级路线与迁移清单
java·spring boot·后端·spring cloud·微服务·后端开发·jdk 21
Maiko Star5 小时前
* LangChain 提示词模板详解:ChatPromptTemplate 的使用与高级特性
java·人工智能·langchain
现任明教教主~7 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
一条破秋裤8 小时前
Linux 线程创建:pthread_create 与基本回收
java·linux·运维
布吉岛的石头8 小时前
Java 程序员第 49 阶段5:BERT 预训练目标 MLM+NSP 的工程含义
java·人工智能·深度学习·bert·transformer
摇滚侠9 小时前
《On Java 中文版 基础卷》阅读笔记 对象无处不在 03
java·笔记·python
二十雨辰9 小时前
[学成在线]-01项目背景
java
弈栈录10 小时前
Java AI 应用的异步化与高并发设计
java·后端·架构
落魄实习生10 小时前
Agent Scope Java 2.x 系列【2】 ReActAgent
java·开发语言
艸肅10 小时前
Ubuntu 安装 JDK (含手动安装)
java·ubuntu·jdk