当前位置:网站首页>文件的上传功能
文件的上传功能
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";
}
}
边栏推荐
- @5-1 CCF 2019-12-1 reporting
- 小程序企业发放红包功能
- [RNN] analyze the RNN from rnn- (simple|lstm) to sequence generation, and then to seq2seq framework (encoder decoder, or seq2seq)
- C函数不加括号的教训
- 用ESP32+TM1638实验NTP网络校时闹钟的ARDUINO代码
- CCF 201503-3 Festival
- ADC introduction
- Armv8 datasheet learning
- File -- first acquaintance
- CCF 201512-4 delivery
猜你喜欢

阿里MQTT物联网平台“云产品流转”实战——两片ESP32通过物联网平台实现远程互操作

ROS distributed operation -- launch file starts nodes on multiple machines

nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE

Introduction to low power consumption and UPF

Probabilistic robot learning notes Chapter 2

手持振弦VH501TC采集仪传感器的连接与数据读取

线程池的设计和原理

CCF 201509-3 template generation system
![[necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.](/img/f5/e6739083f0dce8da1d09d078321633.png)
[necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.
![严重 [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化组件](/img/39/6f6760e80acec0b02028ea2ed1a5b1.png)
严重 [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化组件
随机推荐
Armv8 datasheet learning
LoRA转4G及网关中继器工作原理
Excel导入导出源码分析
@5-1 CCF 2019-12-1 reporting
Mlx90640 infrared thermal imaging sensor temperature measurement module development notes (III)
一个可以返回前一页并自动刷新页面的ASP代码.
Mlx90640 infrared thermal imager temperature measurement module development notes (I)
无线振弦采集仪参数配置工具的设置
Temperature, humidity and light intensity acquisition based on smart cloud platform
多通道振弦、温度、模拟传感信号采集仪数据查看和参数修改
ESP32定时中断实现单、双击、长按等功能的按键状态机Arduino代码
mysql历史数据补充新数据
阿里MQTT物联网平台“云产品流转”实战——两片ESP32通过物联网平台实现远程互操作
testbench简介
BSP3 电力监控仪(功率监控仪)端子定义和接线
JSP详解
¥ 1-1 SWUST OJ 941: implementation of consolidation operation of ordered sequence table
MLX90640 红外热成像仪测温模块开发笔记(四)
Subtotal of rospy odometry sinkhole
Coredata storage to do list