当前位置:网站首页>文件的上传功能
文件的上传功能
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";
}
}
边栏推荐
- Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)
- I2C也可总线取电!
- JSP详解
- Introduction to Verdi Foundation
- salt常见问题
- 一个可以返回前一页并自动刷新页面的ASP代码.
- ESP32连接阿里云MQTT物联网平台
- Introduction to testbench
- JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
- Mlx90640 infrared thermal imager temperature measurement module development instructions
猜你喜欢

Introduction to armv8 architecture

手持振弦采集仪对振弦传感器激励方法和激励电压

¥ 1-2 example 2.2 put the union of two sets into the linear table

Camera attitude estimation

小程序企业发放红包功能

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

nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE

TM1638 LED数码显示模块ARDUINO驱动代码

vscode插件开发

CCF 201509-3 template generation system
随机推荐
OC -- Inheritance and polymorphic and pointer
CCF 201509-3 template generation system
Introduction to Verdi Foundation
Mlx90640 infrared thermal imager temperature measurement module development notes (I)
Swift creates weather app
MLX90640 红外热成像仪测温模块开发笔记(一)
工程监测多通道振弦传感器无线采集仪外接数字传感器过程
ADC介绍
¥ 1-2 example 2.2 put the union of two sets into the linear table
NLM5系列无线振弦传感采集仪的工作模式及休眠模式下状态
ARMv8通用定时器简介
LOAM 融合 IMU 细节之 TransformToEnd 函数
FLASH read / write operation and flash upload file of esp8266
CCF 201604-2 Tetris
概率论与数理统计 4 Continuous Random Variables and Probability Distributions(连续随机变量与概率分布)(上篇)
nodejs版本升级或切换的常用方式
Armv8 datasheet learning
Solve the Chinese garbled code error of qtcreator compiling with vs
vant问题记录
入住阿里云MQTT物联网平台