SpringMVC 文件上传
<!--支持文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--maxUploadSizePerFile:单个文件大小限制
maxUploadSize:整个请求大小限制-->
<property name="maxUploadSizePerFile" value="#{10*1024*1024}"/>
<property name="maxUploadSize" value="#{100*1024*1024}"/>
</bean>
package com.tobie.upfile;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
/**
* @author Tobieance
* @description 上传控制器
* @date 2023-09-14 11:37
*/
@Controller
@RequestMapping("/upload")
public class UploadController {
/**
* <h2>单文件上传</h2>
*
* @param file 文件
* @param modelMap 模型映射
* @return {@link String}
* @throws IOException
*/
@RequestMapping("/upload1")
public String upload1(@RequestParam("file1") MultipartFile file, ModelMap modelMap) throws IOException {
System.out.println(file);
//获取文件名
String originalFileName = file.getOriginalFilename();
System.out.println(originalFileName);
//设置保存文件路径
String path = String.format("E:\\%s", originalFileName);
//创建上传文件实例
File destFile = new File(path);
//写文件
file.transferTo(destFile);
String message = String.format("上传文件<b>%s</b>至<b>%s</b><br/>成功!", originalFileName, path);
modelMap.addAttribute("msg", message);
return "result";
}
/**
* <h2>多文件处理(仅文件)</h2>
*
* @param file1 文件1
* @param file2 文件2
* @param modelMap 模型映射
* @return {@link String}
* @throws IOException
*/
@RequestMapping("/upload2")
public String upload2(MultipartFile file1, MultipartFile file2, ModelMap modelMap) throws IOException {
System.out.println(file1);
System.out.println(file2);
//具体实现与案例1相似
//获取文件名
String originalFileName1 = file1.getOriginalFilename();
String originalFileName2 = file1.getOriginalFilename();
System.out.println(originalFileName1);
System.out.println(originalFileName2);
//设置保存文件路径
String path1 = String.format("E:\\%s", originalFileName1);
String path2 = String.format("E:\\%s", originalFileName2);
//创建上传文件实例
File destFile1 = new File(path1);
File destFile2 = new File(path2);
//写文件
file1.transferTo(destFile1);
file2.transferTo(destFile2);
String message = String.format("上传文件 <b>%s</b> 至 <b>%s</b> 成功!<br/>" +
"上传文件 <b>%s</b> 至 <b>%s</b> 成功!", originalFileName1, path1, originalFileName2, path2);
modelMap.addAttribute("msg", message);
return "result";
}
/**
* <h2>多文件处理(带普通控件)</h2>
*
* @param request
* @param modelMap
* @return {@link String}
* @throws IOException
*/
@RequestMapping("/upload3")
public String upload3(MultipartHttpServletRequest request, ModelMap modelMap) throws IOException {
//TODO:获取普通控件数据
System.out.println("---------获取普通控件数据--------");
//key:控件的name属性 value:控件属性值(可能有多个值)
Map<String, String[]> parameterMap = request.getParameterMap();
parameterMap.forEach((name, values) -> {
System.out.println(String.format("%s:%s", name, Arrays.asList(values)));
});
//TODO:获取文件数据
//key:控件的name属性 value:文件数据
MultiValueMap<String, MultipartFile> multiValueMap = request.getMultiFileMap();
multiValueMap.forEach((name, file) -> {
System.out.println(String.format("%s:\n%s", name, file));
});
String message = String.format("上传文件成功!");
modelMap.addAttribute("msg", message);
return "result";
}
@RequestMapping("/upload4")
public String upload4(@ModelAttribute("msg")UserDto userDto) throws IOException {
System.out.println(userDto);
return "result";
}
}
标签:文件,String,SpringMVC,System,println,import,上传,out
From: https://www.cnblogs.com/tobieance/p/17744461.html