Spring MVC的MultipartFile

定义

MultipartFile接口是Spring MVC中用来处理上传文件的接口,它提供了访问上传文件内容、文件名称、文件大小等信息的方法。

源码:

java 复制代码
package org.springframework.web.multipart;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.springframework.core.io.InputStreamSource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.FileCopyUtils;

public interface MultipartFile extends InputStreamSource {
    String getName();

    @Nullable
    String getOriginalFilename();

    @Nullable
    String getContentType();

    boolean isEmpty();

    long getSize();

    byte[] getBytes() throws IOException;

    InputStream getInputStream() throws IOException;

    default Resource getResource() {
        return new MultipartFileResource(this);
    }

    void transferTo(File dest) throws IOException, IllegalStateException;

    default void transferTo(Path dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest));
    }
}

方法

getOriginalFilename():获取上传文件的原始名称,包括文件的扩展名。

transferTo(File destination):将上传的文件保存到指定的目标路径。

相关推荐
oliveira-time1 分钟前
ArrayList和LinkedList区别
java·开发语言
潮流coder4 分钟前
IntelliJ IDEA给Controller、Service、Mapper不同文件设置不同的文件头注释模板、Velocity模板引擎
java·ide·intellij-idea
lichuangcsdn7 分钟前
【springcloud学习(dalston.sr1)】项目整体介绍(含源代码)(一)
学习·spring·spring cloud
码农飞哥10 分钟前
互联网大厂Java求职面试实战:Spring Boot与微服务场景深度解析
java·数据库·spring boot·安全·微服务·消息队列·互联网医疗
Akiiiira27 分钟前
【日撸 Java 300行】Day 14(栈)
java·开发语言
猴子请来的逗比48934 分钟前
tomcat与nginx之间实现多级代理
java·nginx·tomcat
一丝晨光37 分钟前
数值溢出保护?数值溢出应该是多少?Swift如何让整数计算溢出不抛出异常?类型最大值和最小值?
java·javascript·c++·rust·go·c·swift
意倾城41 分钟前
浅说MyBatis-Plus 的 saveBatch 方法
java·mybatis
JANYI20181 小时前
C语言易混淆知识点详解
java·c语言·算法
kyy_studydiary1 小时前
集合-进阶
java·开发语言