SpringBoot上如何实现文件上传 FILE

转:SpringBoot上如何实现文件上传(config文件限制上传文件的大小)

2.利用Spring-boot实现文件上传功能,将多文件上传至服务器!

3.Springboot实现多文件上传功能

  相比于单文件上传,这里就多了一个遍历的过程。

4.Spring Boot多文件上传

 

5.SpringBoot 文件上传临时文件路径配置详解

  由于临时/tmp目录下的文件,在长时间(10天)没有使用的情况下,就会被系统机制自动删除掉。

 
  

  解决方案:统一使用 “Content-Type:application/json”格式,后台通过key来接受json串

 9.使用RequestPart 来解决传参问题

  @RequestParam和@RequestPart的区别

  当请求头中指定Content-Type:multipart/form-data时,传递的json参数,@RequestPart注解可以用对象来接收,@RequestParam只能用字符串接收

 

10.判断MultipartFile是否为空

import org.springframework.util.ObjectUtils;

ObjectUtils.isEmpty(file);

public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}

11.File类的createNewFile()和mkdirs() mkdir()

  createNewFile 创建文件的 前提路径需要存在

  mkdir:只能用来创建文件夹,且只能创建一级目录,如果上级不存在,就会创建失败。
  mkdirs:只能用来创建文件夹,且能创建多级目录 ,如果上级不存在,就会自动创建。(创建文件夹多用此)
  createNewFile:只能用来创建文件,且只能在已存在的目录下创建文件,否则会创建失败。(FileOutputStream os=new FileOutputStream(file)也可创建文件,看情况使用)

12.使用File类的file.exists()方法检查文件的存在。

13.SpringBoot 2.X 设置文件上传大小

 14.Spring Boot 2.0设置上传文件大小

原因:配置文件版本问题

在application.properties中的配置为

SpringBoot-2.0:

spring.servlet.multipart.max-file-size=10Mb

spring.servlet.multipart.max-request-size=100Mb

SpringBoot-1.4:

spring.http.multipart.maxFileSize=10Mb

spring.http.multipart.maxRequestSize=100Mb

SpringBoot-1.4之前:

multipart.maxFileSize = 10Mb

multipart.maxRequestSize=100Mb

 

 

效果:springboot 2.x上 上传文件创建临时文件夹,并限制上传大小为1k

 

 

代码:

复制代码
----yml
location:
  tempDirLinux: /opt/location/tempDir 
  tempDirWindows: D:\tempDir 
  filePathLinux: /opt/location/filePath
  filePathWindows: D:\filePath

---- config

package com.sinosoft.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * TODO
 *
 * @author zhaoJs
 * @version 1.0
 * @date 2022/6/9 10:11
 */
@Configuration
public class MultipartConfig {

    @Value("${location.tempDirLinux}")
    private String tempDirLinux;

    @Value("${location.tempDirWindows}")
    private String tempDirWindows;

    @Bean
    MultipartConfigElement multipartConfigElement() {
        String tempDir = null;
        String osName = System.getProperties().getProperty("os.name");
        if (osName.equalsIgnoreCase("Linux")) {
            tempDir = tempDirLinux;
        } else {
            tempDir = tempDirWindows;
        }

        MultipartConfigFactory factory = new MultipartConfigFactory();
        File tmpDirFile = new File(tempDir);
        // 判断文件夹是否存在
        if (!tmpDirFile.exists()) {
            tmpDirFile.mkdirs();
        }
        factory.setLocation(tempDir);
        factory.setMaxFileSize(DataSize.of(10, DataUnit.MEGABYTES));
        factory.setMaxRequestSize(DataSize.of(10, DataUnit.MEGABYTES));
        return factory.createMultipartConfig();
    }
}
复制代码

 

posted @   BBS_自律  阅读(626)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
点击右上角即可分享
微信分享提示