当前位置:网站首页>文件的上传功能
文件的上传功能
2022-07-25 09:25:00 【今天你学习了ma】

使用文件上传时,设置 enctype="multipart/form-data" 并且method为post请求否则无法提交
上传多个文件时,则需要在后面加上 multiple

在对应的controller层中取出表单中的数据时,取出普通的参数可以使用@RequestParam注解
取出文件可以使用@RequestPart注解 文件类型为MultipartFile,若有多个文件则需要用MultipartFile[] 数组存储.
若需要将文件保存在服务器中可以使用springboot封装的对象直接调用, transferTo方法
默认单个文件最大大小为1MB...最大的请求的大小为10MB

max-file-size: 10MB 设置单个文件最大大小 max-request-size: 100MB 设置最大的请求的大小
若文件大小超出则需要在yml配置文件文件中修改

package com.zwz.springbootweb.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
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.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@Slf4j
@Controller
public class FileController {
@RequestMapping("/file")
public String intoFileLoad(){
return "fileLoad";
}
@PostMapping("/fileLoad")
public String fileLoad(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestPart("photo") MultipartFile photo,
@RequestPart("photos") MultipartFile[] photos) throws IOException {
log.info(username);
log.info(password);
if(!photo.isEmpty()){
String filename = photo.getOriginalFilename();
photo.transferTo(new File("E:\\ideaCache\\"+filename));
}
for (MultipartFile pho : photos) {
if(!pho.isEmpty()){
String filename = pho.getOriginalFilename();
pho.transferTo(new File("E:\\ideaCache\\"+filename));
}
}
return "fileInputSuccess";
}
}
边栏推荐
猜你喜欢

ESP32定时中断实现单、双击、长按等功能的按键状态机Arduino代码

OC -- Inheritance and polymorphic and pointer

VS无线振弦采集仪蓝牙功能的使用

Mlx90640 infrared thermal imager temperature measurement module development notes (4)

Qt 6.2的下载和安装

ThreadLocal&Fork/Join

LoRA转4G及网关中继器工作原理

Connection and data reading of hand-held vibrating wire vh501tc collector sensor

概率论与数理统计 4 Continuous Random Variables and Probability Distributions(连续随机变量与概率分布)(上篇)

手持振弦VH501TC采集仪传感器的连接与数据读取
随机推荐
emmet语法速查 syntax基本语法部分
MLX90640 红外热成像仪测温模块开发笔记(五)
First knowledge of opencv4.x --- box filtering
小程序调起微信支付
[machine translation] scones -- machine translation with multi tag tasks
CCF 201509-3 template generation system
js利用requestAnimationFrame实时检测当前动画的FPS帧率
OC -- Inheritance and polymorphic and pointer
Arm preliminaries
oracle 解析同名xml 问题
ADC介绍
手持振弦VH501TC采集仪传感器的连接与数据读取
mysql历史数据补充新数据
File -- first acquaintance
Redux使用和剖析
nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE
工程仪器振弦传感器无线采集仪的采集数据发送方式及在线监测系统
Mlx90640 infrared thermal imager temperature measurement module development notes (4)
Introduction to low power consumption and UPF
MVC三层架构理解