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());
    }
}
相关推荐
一 乐1 天前
电影院|基于springboot + vue电影院购票管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·电影院购票管理管理系统
恼书:-(空寄1 天前
JVM GC 日志分析 + 常见 GC 场景 + 实战参数调优
java·jvm
消失的旧时光-19431 天前
Spring Boot 实战(五):接口工程化升级(统一返回 + 异常处理 + 错误码体系 + 异常流转机制)
java·spring boot·后端·解耦
杨凯凡1 天前
【012】图与最短路径:了解即可
java·数据结构
比特森林探险记1 天前
【无标题】
java·前端
椰猫子1 天前
Javaweb(Filter、Listener、AJAX、JSON)
java·开发语言
朝新_1 天前
【Spring AI 】核心知识体系梳理:从入门到实战
java·人工智能·spring
一 乐1 天前
旅游|基于springboot + vue旅游信息推荐系统(源码+数据库+文档)
java·vue.js·spring boot·论文·旅游·毕设·旅游信息推荐系统
我命由我123451 天前
Android 开发中,关于 Gradle 的 distributionUrl 的一些问题
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
橙露1 天前
SpringBoot 全局异常处理:优雅封装统一返回格式
java·spring boot·后端