但是这一切的前提都是穆尔斯基提督的舰队还能拦截住刘皓他们才行,不然的话一切都是空口说白话,纸上谈兵而已。

Spring Boot 文件上传原理


  首先我们要知道什么是Spring Boot,这里简单说一下,Spring Boot可以看作是一个框架中的框架--->集成了各种框架,像security、jpa、data、cloud等等,它无须关心配置可以快速启动开发,有兴趣可以了解下自动化配置实现原理,本质上是 spring 4.0的条件化配置实现,深抛下注解,就会看到了。

  说Spring Boot 文件上传原理 其实就是Spring MVC,因为这部分工作是Spring MVC做的而不是Spring Boot,那么,SpringMVC又是怎么处理文件上传这个过程的呢?

  图:

  首先项目启动加载相关配置,再执行上述第二步的时候 DispatcherServlet会去查找id为multipartResolver的Bean,在配置中看到Bean指向的是CommonsMultipartResolve,其中实现了MultipartResolver接口。

  第四步骤这里会判断是否multipart文件即isMultipart方法,返回true:就会调用 multipartResolver 方法,传递HttpServletRequest会返回一个MultipartHttpServletRequest对象,再由DispatcherServlet进行处理到Controller层;返回false:会忽略掉,继续传递HttpServletRequest。

  在MVC中需要在配置文件webApplicationContext.xml中配置 如下:

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="UTF-8"/>
            <property name="maxUploadSize" value="100000000"/>
            <property name="uploadTempDir" value="fileUpload/temp"/>
    </bean>

  而Spring Boot已经自动配置好,直接用就行,做个test没什么问题,有默认的上传限制大小(maxFileSize = "1MB",maxRequestSize = "10MB",fileSizeThreshold = "0"),不过在实际开发中我们还是要做一些配置的,

如下在application.properties中:

# multipart config
#默认支持文件上传 spring.http.multipart.enabled=true
#文件上传目录 spring.http.multipart.location=/tmp/file/images/
#将文件写入磁盘的阈值 spring.http.multipart.file-size-threshold=5MB
#最大支持请求大小
spring.http.multipart.max-request-size=20MB

当然也可以写配置类来实现,具体的就不做展示了。

  看完上述你肯定有个大概的了解了,这里再啰嗦下,Spring提供Multipart的解析器:MultipartResolver,上述说的是CommonsMultipartResolver,它是基于Commons File Upload第三方来实现,这也是在Servlet3.0之前的东西,3.0+之后也可以不需要依赖第三方库,可以用StandardServletMultipartResolver,同样也是实现了MultipartResolver接口,我们可以看下它的实现源码(上有注释,不再进行解析了):

 * Copyright 2002-2017 the original author or authors.

package org.springframework.web.multipart.support;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

import org.apache.commons.logging.LogFactory;

import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;

/**
 * Standard implementation of the {@link MultipartResolver} interface,
 * based on the Servlet 3.0 {@link javax.servlet.http.Part} API.
 * To be added as "multipartResolver" bean to a Spring DispatcherServlet context,
 * without any extra configuration at the bean level (see below).
 *
 * <p><b>Note:</b> In order to use Servlet 3.0 based multipart parsing,
 * you need to mark the affected servlet with a "multipart-config" section in
 * {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement}
 * in programmatic servlet registration, or (in case of a custom servlet class)
 * possibly with a {@link javax.servlet.annotation.MultipartConfig} annotation
 * on your servlet class. Configuration settings such as maximum sizes or
 * storage locations need to be applied at that servlet registration level;
 * Servlet 3.0 does not allow for them to be set at the MultipartResolver level.
 *
 * @author Juergen Hoeller
 * @since 3.1
 * @see #setResolveLazily
 * @see HttpServletRequest#getParts()
 * @see org.springframework.web.multipart.commons.CommonsMultipartResolver
 */
public class StandardServletMultipartResolver implements MultipartResolver {

    private boolean resolveLazily = false;


    /**
     * Set whether to resolve the multipart request lazily at the time of
     * file or parameter access.
     * <p>Default is "false", resolving the multipart elements immediately, throwing
     * corresponding exceptions at the time of the {@link #resolveMultipart} call.
     * Switch this to "true" for lazy multipart parsing, throwing parse exceptions
     * once the application attempts to obtain multipart files or parameters.
     */
    public void setResolveLazily(boolean resolveLazily) {
        this.resolveLazily = resolveLazily;
    }


    @Override
    public boolean isMultipart(HttpServletRequest request) {
        // Same check as in Commons FileUpload...
        if (!"post".equals(request.getMethod().toLowerCase())) {
            return false;
        }
        String contentType = request.getContentType();
        return (contentType != null && contentType.toLowerCase().startsWith("multipart/"));
    }

    @Override
    public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
        return new StandardMultipartHttpServletRequest(request, this.resolveLazily);
    }

    @Override
    public void cleanupMultipart(MultipartHttpServletRequest request) {
        // To be on the safe side: explicitly delete the parts,
        // but only actual file parts (for Resin compatibility)
        try {
            for (Part part : request.getParts()) {
                if (request.getFile(part.getName()) != null) {
                    part.delete();
                }
            }
        }
        catch (Throwable ex) {
            LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex);
        }
    }

}
View Code

这里是之前写的test后者实现配置类,可以简单看下,作为了解:

package com.bj.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.MultipartProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;

@Configuration
@EnableConfigurationProperties(MultipartProperties.class)
public class FileUploadConfig {

    private final MultipartProperties multipartProperties;

    public FileUploadConfig(MultipartProperties multipartProperties){
        this.multipartProperties=multipartProperties;
    }

    /**
     * 注册解析器
     * @return
     */
    @Bean(name= DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
    @ConditionalOnMissingBean(MultipartResolver.class)
    public StandardServletMultipartResolver multipartResolver(){
        StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();
        multipartResolver.setResolveLazily(multipartProperties.isResolveLazily());
        return multipartResolver;
    }

    /**
     * 上传配置
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public MultipartConfigElement multipartConfigElement(){
        return this.multipartProperties.createMultipartConfig();
    }

}
View Code

 

当前文章:http://hnhdqp.com/play/mkx9dykd9n.html

发布时间:2019-01-22 14:41:55

可兑换捕鱼棋牌下载 领域棋牌怎么提现 大小球软件 扑克牌捡红点三个人玩 纸牌app 王者炸金花坑了多少人 亲朋棋牌极速捕鱼作弊 推广棋牌娱乐 山东体彩11选5走势图 3366全民斗地主

编辑:龙平顺平

相关新闻

榆林公检法:举报涉爆涉枪违法犯罪最高奖10万元

2019-01-22 16:45:37

克孜勒苏戮衙集团有限责任公司

创维注重白电品质冰箱复查合格

2019-01-22 19:50:25

章丘潮卤商务服务有限公司

神东上半年科研成果丰硕 开展项目200项 申报专利60项

2019-01-22 13:28:53

扬州季帽才通讯股份有限公司

热门推荐

  • 真三国无双8简中版2月8日推出 PS4标准版售价349元
  • 程序员面试所需的软技能(二)
  • 特斯拉头五年:马斯克还不是CEO,一代车刚量产……
  • 保时捷新Cayenne国内上市 售99.8-199.8万元
  • 特斯拉电动半挂卡车明天发布:哪些性能值得期待?
  • 肇庆这个地方的婚嫁习俗很独特?!快来瞧瞧~
  • 《银魂乱舞》繁体中文宣传片 游戏1.18同步发售
  • 搭载2.0L动力 现代新ix35将于11月15日上市
  • OOAD-设计模式(四)结构型模式之适配器、装饰器、代理模式
  • 美联邦法官三度叫停旅行限令 特朗普政府正式上诉
  • 河北新闻网版权所有 本站点信息未经允许不得复制或镜像 法律顾问:787棋牌去哪了 标准的扑克大小王
  • 神扑克怎么玩的那么大 copyright ? 2000 - 2016
  • 新闻热线:0311-67563366 广告热线:0311-67562966 新闻投诉:0311-67562994
  • 冀ICP备 09047539号-1 | 互联网新闻信息服务许可证编号:1312006002
  • 广播电视节目制作经营许可证(冀)字第101号|信息网络传播视听节目许可证0311618号
  • 手机麻将赢现金提现 信誉棋牌评测网大全 一木棋牌客服不回话 真人赢钱提现金的游戏