基于SpringBoot实现简单图片上传

一、SpringBoot

       Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

二、SpringBoot启动原理

       SpringBoot整个启动流程分为两个步骤:
       1、初始化一个SpringApplication对象
       2、执行该对象的run方法。

       SpringApplication初始化

public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    this.webApplicationType = deduceWebApplicationType();
    setInitializers((Collection) getSpringFactoriesInstances(
            ApplicationContextInitializer.class));
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = deduceMainApplicationClass();
}

       SpringApplication.run方法

public ConfigurableApplicationContext run(String... args) {
    //计时
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    //spring容器
    ConfigurableApplicationContext context = null;
    //错误回调
    Collection exceptionReporters = new ArrayList<>();
    //设置一些系统属性
    configureHeadlessProperty();
    //获取启动时监听器
    SpringApplicationRunListeners listeners = getRunListeners(args);
    //启动监听器
    listeners.starting();
    try {
        //获取一些启动参数
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        //创建运行环境environment
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        //设置一些系统参数
        configureIgnoreBeanInfo(environment);
        //打印banner
        Banner printedBanner = printBanner(environment);
        //创建spring容器
        context = createApplicationContext();
        //获取异常报告,回调
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                new Class[] { ConfigurableApplicationContext.class }, context);
        //准备容器
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        //刷新容器
        refreshContext(context);
        //spring容器后置处理
        afterRefresh(context, applicationArguments);
        //计时终止
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        //结束通知
        listeners.started(context);
        //执行runner
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }

    try {
        //spring容器就绪通知
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    //返回容器
    return context;
}

三、实现一个简单SpringBoot项目(图片上传)

       1、页面代码
       一段简单的JSP页面代码,注: method=“post” enctype="multipart/form-data"是必须要加的,不然会报错。

请选择上传的图片或文件:
<%----%>

       2、控制层

@RequestMapping(value = "upload")
public ModelAndView upload(@RequestParam(value = "file") MultipartFile file){
    ModelAndView modelAndView = new ModelAndView();
    if (file.isEmpty()) {
        System.out.println("文件为空");
    }
    String fileName = file.getOriginalFilename();  // 文件名
    String filePath = "F:\\Workspase\\BackController\\src\\main\\webapp\\jsp\\pic\\"; // 上传后的路径
    //fileName = UUID.randomUUID() + suffixName; // 存库的时候使用
    File dest = new File(filePath + fileName);
    if (!dest.getParentFile().exists()) {
        dest.getParentFile().mkdirs();
    }
    try {
        file.transferTo(dest);
    } catch (IOException e) {
        e.printStackTrace();
    }
    modelAndView.addObject("filename","jsp/pic/"+fileName);
    modelAndView.setViewName("dong");
    return modelAndView;
}

ps:针对图片无法回显问题,首先检查一下路径写的对不对!!! 这个是最重要的。如果路径没问题,可能是拦截器的问题,在application.properties中加上静态资源访问路径就可以了。

spring.resources.static-locations=/jsp/pic/**

xDroid——让安卓应用运行在Linux平台上

你可能感兴趣的:(Java,SpringBoot,图片上传,Java)