SpringMVC——文件上传下载
2024-09-05 07:54:33
一、单文件上传
1.导入依赖
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
2.创建文件上传表单
<form action="/file/fileUpload" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> 作者: <input type="text" name="author"/> <input type="submit" value="提交"/> </form>
3.构建文件上传控制器
@RequestMapping("/fileUpload") /** * MultipartFile 选择的文件 */ public String fileupload(HttpSession session,MultipartFile file,String author) throws IOException { System.out.println("作者:"+author); System.out.println(file); /** * 如何处理文件 */ if(!file.isEmpty()){ //获取文件名称 String fileName=file..getOriginalFilename(); //获取到需要上传的路径 String realPath = session.getServletContext().getRealPath("/WEB-INF/upload"); //创建文件对象 File uploadfile=new File(realPath+"\\"+fileName); //如何上传文件 file.transferTo(uploadfile); } return "index"; }
4.注册文件上传解析器
<!--文件上传解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--编码--> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="500000"/> </bean>
二、多文件上传
文件上传表单:
<form action="/file/fileUploads" method="post" enctype="multipart/form-data"> <input type="file" name="uploadFiles"/> <input type="file" name="uploadFiles"/> <input type="file" name="uploadFiles"/> 作者: <input type="text" name="author"/> <input type="submit" value="提交"/> </form>
控制器
@RequestMapping("/fileUploads") /** * MultipartFile 选择的文件 */ public String fileUploads(HttpSession session,MultipartFile[] uploadFiles,String author) throws IOException { System.out.println("作者:"+author); System.out.println(uploadFiles); /** * 如何处理文件 */ for (MultipartFile file:uploadFiles){ if(!file.isEmpty()){ //获取文件名称 String fileName=file.getOriginalFilename(); //获取到需要上传的路径 String realPath = session.getServletContext().getRealPath("/WEB-INF/upload"); //创建文件对象 File uploadfile=new File(realPath+"\\"+fileName); //如何上传文件 file.transferTo(uploadfile); } } return "index"; }
其余一致
三、文件下载
后期补
最新文章
- 【源码】Word转PDF V1.0.1 小软件,供新手参考
- SQL Server编程(04)基本语法【转载】
- js的变量使用<;bean>;<;list:write>;赋值时需加&#39; &#39;
- javascript与服务器3
- WIN7下使用VC2010调试uCOS-II 2.91
- LeetCode-Repeated DNA Sequences (位图算法减少内存)
- Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning
- u盘安装centos
- iOS获取手机相关信息
- hdu 3948(后缀数组+RMQ)
- stdio.h及cstdio的区别
- 待修改 nyoj 412 又是一个遗留问题
- [问题解决] ubuntu server12.04 按ctrl+alt+F1没用
- 本地环境下 WordPress 环境搭建与安装
- hibernate ——联合主键
- python!!!!惊了,这世上居然还有这么神奇的东西存在
- 全文检索-Elasticsearch (四) elasticsearch.net 客户端
- October 16th 2017 Week 42nd Monday
- spring Boot使用AOP统一处理Web请求日志记录
- jquery cdn加速注意事项
热门文章
- LeetCode 61——旋转链表(JAVA)
- Java多线程(十):BlockingQueue实现生产者消费者模型
- 按Excel的模板导出数据
- WPF DataGrid数据绑定
- c#如何使用MemoryStream和BinaryFormatter进行对象的序列化和返序列化
- js 扁平化输出数组
- 【python+beautifulsoup4】Python中安装bs4后,pycharm报错ModuleNotFoundError: No module named &#39;bs4&#39;
- Async 配置线程池
- es string 分词完整示例
- Django_06_项目完成