SpringMVC怎么实现文件的上传和下载
发表于:2025-11-16 作者:千家信息网编辑
千家信息网最后更新 2025年11月16日,这篇文章给大家分享的是有关SpringMVC怎么实现文件的上传和下载的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。具体内容如下0.环境准备1.maven依赖
千家信息网最后更新 2025年11月16日SpringMVC怎么实现文件的上传和下载0.环境准备
1.文件上传
这篇文章给大家分享的是有关SpringMVC怎么实现文件的上传和下载的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
具体内容如下
0.环境准备
1.maven依赖
org.junit.jupiter junit-jupiter-api 5.7.0 test javax.servlet javax.servlet-api 3.1.0 provided org.springframework spring-webmvc 5.2.6.RELEASE commons-io commons-io 2.8.0 commons-fileupload commons-fileupload 1.3.3 2.springConfig。xml配置文件
3.web.xml配置
web org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springConfig.xml 1 web *.mvc characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 forRequestEncoding true forResponseEncoding true characterEncodingFilter /* 1.文件上传
文件上传分为三种方式:
单个文件单字段
多个文件单字段
多个文件多字段
注意点:
1、提交方式为表单的post请求
2、from属性中必须有enctype="multipart/form-data"
3、如果是单字段多文件:输入框中的属性必须为:multiple="multiple"
4、表单中的属性name必须和后端参数一致
1.前端代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 文件上传 文件上传(单个文件单字段上传)
文件上传(多文件单字段上传)
文件上传(多文件多字段上传)
2.后端代码
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpSession;import java.io.File;import java.io.IOException;/** * @author compass * @version 1.0 * @date 2021-05-11 14:33 */@Controllerpublic class UploadIFileController { // 处理单个文件上传 @PostMapping("/uploadFile1.mvc") public ModelAndView uploadFile1(MultipartFile file, HttpSession session) throws IOException { ModelAndView view = new ModelAndView(); // 得到文件名称 String filename=file.getOriginalFilename(); System.out.println("文件名称:"+filename); if (!file.isEmpty()){ // 判断文件的后缀 if (filename.endsWith(".jpg")||filename.endsWith(".png")||filename.endsWith(".txt")); //设置文件的保存路径 String savePath="C:\Users\14823\IdeaProjects\springMVC\fileupload\src\main\webapp\WEB-INF\file"; File srcFile = new File(savePath,filename); // 执行文件保存操作 file.transferTo(srcFile); view.setViewName("forward:/uploadSuccess.jsp"); }else { view.setViewName("forward:/uploadFailed.jsp"); } return view; } // 处理多文件上传 @PostMapping("/uploadFile2.mvc") public ModelAndView uploadFile2(MultipartFile[] file,HttpSession session) throws IOException { ModelAndView view = new ModelAndView(); //设置文件的保存路径 String savePath="C:\Users\14823\IdeaProjects\springMVC\fileupload\src\main\webapp\WEB-INF\file"; String[] filenames = new String[file.length]; // 只要上传过来的文件为空或者是不符合指定类型的都会上传失败 for (int i = 0; i 2.文件下载
文件下分为两种情况:
文件名称是纯英文字母的
文件名带有中文不处理会乱码的
1.前端代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 文件下载 文件下载(非中文名称)
文件下载(中文名称)
2.后端代码
/** * @author compass * @version 1.0 * @date 2021-05-11 15:23 */@Controllerpublic class DownloadController { // 非中文名称文件下载 @GetMapping("/download1.mvc") public ResponseEntity fileDownload1(String filename,HttpServletRequest request) throws IOException { String path="C:\Users\14823\IdeaProjects\springMVC\fileupload\src\main\webapp\WEB-INF\file\"; File file = new File(path,filename); HttpHeaders header = new HttpHeaders(); header.setContentDispositionFormData("attachment",filename); header.setContentType(MediaType.APPLICATION_OCTET_STREAM); ResponseEntity result = new ResponseEntity<>(FileUtils.readFileToByteArray(file), header, HttpStatus.OK); return result; } // 中文名称文件下载 @GetMapping("/download2.mvc") public ResponseEntity fileDownload2(String filename,HttpServletRequest request) throws IOException { System.out.println(filename); String path="C:\Users\14823\IdeaProjects\springMVC\fileupload\src\main\webapp\WEB-INF\file\"; filename = filename.replace("_", "%"); filename= URLDecoder.decode(filename,"UTF-8"); String downloadFile=""; if (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie")>0){ filename= URLEncoder.encode(filename,"UTF-8"); downloadFile=filename.replaceAll("+","%20"); }else { downloadFile=new String(filename.getBytes("UTF-8"),"ISO-8859-1"); } File file = new File(path,filename); HttpHeaders header = new HttpHeaders(); header.setContentDispositionFormData("attachment",downloadFile); header.setContentType(MediaType.APPLICATION_OCTET_STREAM); ResponseEntity result = new ResponseEntity<>(FileUtils.readFileToByteArray(file), header, HttpStatus.OK); return result; }} 感谢各位的阅读!关于"SpringMVC怎么实现文件的上传和下载"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
文件
名称
字段
文件下载
中文
代码
内容
单个
属性
UTF-8
前端
多个
方式
更多
篇文章
类型
表单
路径
处理
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
是网络安全还是Java好
山东能源供应链服务软件开发商
阿里云服务器价格对照表
查看本地服务器所有端口
t3数据库 无法安装
php网站的数据库在哪
猎豹移动校招软件开发工资
亿家安互联网科技怎么样
德州网络安全等级保护
北京企业软件开发价钱
罕王集团有软件开发
bno数据库什么意思
安徽黑桃互联网科技有限公司
lamp需要购买服务器吗
工业园区便宜服务器应用范围
阿里云服务器怎么下载证书
健鼎内部软件开发
服务器的管理端怎么连
数据库中怎么用生日查询年龄
人民银行科技处与互联网企业
mc1.7.10服务器
上位机数据库
网络安全征文30字
网络安全工程pdf下载
宣城电信软件开发工程师
连接远程数据库服务器名称
泰国 美国 网络安全
个人防护服务器设计图
人民银行科技处与互联网企业
数据库将原表插入新表