springboot的文件上传

本文介绍如何使用SpringBoot和Thymeleaf实现文件上传功能,包括单文件和多文件上传,详细展示了配置依赖、设置上传限制及实现上传逻辑的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot的文件上传

本实例使用"thymeleaf"模板来实现数据的上传。
pom

<dependencies>
  	
  		<!-- thymeleaf模板依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
        <!-- 使用springmvc和spring的jar -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		 <!-- 解决thymeleaf使用非严格的html出现的异常 -->
		  <dependency>
		        <groupId>net.sourceforge.nekohtml</groupId>
		        <artifactId>nekohtml</artifactId>
		        <version>1.9.22</version>
		  </dependency>
  	
  </dependencies>
#设置上传单个文件的大小
multipart.maxFileSize=500Mb
#设置上传中文件的大小
multipart.maxRequestSize=1000Mb


#springboot整合thymeleaf
#关闭thymeleaf缓存 开发时使用 否则没有实时画面
spring.thymeleaf.cache=false
## 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template-location=true
#Content-Type值
spring.thymeleaf.content-type=text/html
#启用MVC Thymeleaf视图分辨率
spring.thymeleaf.enabled=true
#应该从解决方案中排除的视图名称的逗号分隔列表
spring.thymeleaf.excluded-view-names=
#模板编码
spring.thymeleaf.mode=LEGACYHTML5
# 在构建URL时预先查看名称的前缀
spring.thymeleaf.prefix=classpath:/templates/
# 构建URL时附加查看名称的后缀.
spring.thymeleaf.suffix=.html
# 链中模板解析器的顺序
#spring.thymeleaf.template-resolver-order= o
# 可以解析的视图名称的逗号分隔列表
#spring.thymeleaf.view-names=
#thymeleaf end

update.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
	<h2>文件上传</h2>
	<hr/>
	<form action="/update" method="post" enctype="multipart/form-data">
		
		<p>
			文件:<input type="file" name="file" />
		</p>
		<p>
			<input type="submit" value="上传"/>
		</p>
	</form>
	<hr/>
	
	<form action="/uploads" method="post" enctype="multipart/form-data">
		
		<p>
			文件1:<input type="file" name="files" />
		</p>
		<p>
			文件2:<input type="file" name="files" />
		</p>
		<p>
			文件3:<input type="file" name="files" />
		</p>
		<p>
			<input type="submit" value="上传"/>
		</p>
	</form>

</body>
</html>
import java.io.File;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UpdateController {
	
	@RequestMapping("/index")
	public String toUpdate(){
		return "update";
	}
	
	@RequestMapping("/indexs")
	public String toUpdates(){
		return "update";
	}
	
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public String updateFile(MultipartFile file,HttpServletRequest request){
		/*
		 * file参数名必须与form表单中的属性名一致。
		 */
		try {
			//创建文件在服务器端的存放路径
			String dir = request.getServletContext().getRealPath("/update");
			//判断并创建文件夹
			File fileDir = new File(dir);
			if(!fileDir.exists()){
				fileDir.mkdirs();
			}
			
			//生成文件在服务器存放的名字
			//获取文件的扩展名
			String  fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			//生成新的文件名
			String fileName = UUID.randomUUID().toString()+fileSuffix;
			File files = new File(fileDir + "/"+ fileName);
			//实现上传
			file.transferTo(files);
			
		} catch (Exception e) {
			e.printStackTrace();
			return "文件上传失败";
		}
		return "文件上传成功";
	}
	
	//上传多个文件
	@RequestMapping(value="/uploads",method=RequestMethod.POST)
	@ResponseBody
	public String updateFiles(MultipartFile[] files,HttpServletRequest request){
		/*
		 * files参数名必须与form表单中的属性("name")名一致。
		 */
		try {
			//创建文件在服务器端的存放路径
			String dir = request.getServletContext().getRealPath("/update");
			//判断并创建文件夹
			File fileDir = new File(dir);
			if(!fileDir.exists()){
				fileDir.mkdirs();
			}
			
			//生成文件在服务器存放的名字
			//获取文件的扩展名
			for(int i = 0; i < files.length; i++){
				String  fileSuffix = files[i].getOriginalFilename().substring(files[i].getOriginalFilename().lastIndexOf("."));
				//生成新的文件名
				String fileName = UUID.randomUUID().toString()+fileSuffix;
				File file = new File(fileDir + "/"+ fileName);
				//实现上传
				files[i].transferTo(file);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return "文件上传失败";
		}
		return "文件上传成功";
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值