Spring MVC 如何上传多个文件到指定位置
来源:互联网 发布:最新网络伤感流行歌曲 编辑:程序博客网 时间:2024/06/05 22:19
Spring MVC 如何上传多个文件到指定位置
太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)
本文遵循“署名-非商业用途-保持一致”创作公用协议
转载请保留此句:太阳火神的美丽人生 - 本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
SHORT LINK: Last Updated on February 10th, 2015 by Crunchify 22 CommentsSpring MVC Tutorial: How to Upload Multiple Files to Specific Location
This is another complete Spring MVC tutorial which accepts file on Upload form and copy it to specificfolder on “Submit” event. As usual we have a dependency
on Hello World Spring MVC Example.
So, these are the additions / changes we need to perform in this example:
- New file: CrunchifyFileUploadController.java
- New file: CrunchifyFileUpload.java
- New file: uploadfile.jsp
- New file: uploadfilesuccess.jsp
- Modified file: crunchify-servlet.xml
- 2 new jar files:
commons-io-2.4.jar
andcommons-fileupload-1.3.jar
Here is a final project structure so you will get some idea on where to add files.
Now let’s get started:
Step1: Pre-Requisite:
http://crunchify.com/hello-world-example-spring-mvc-3-2-1/ (Deploy this project successfully onTomcat)
Maven Dependencies:
Add below new dependencies to your project’s pom.xml
file.
Step2: SpringController
Create a Spring 3 MVC based controller which handles file upload. There are two methods in thiscontroller:
crunchifyDisplayForm
– It simply forwards request to the pageuploadfile.jspcrunchifySave
– Fetches the form using@ModelAttribute
annotation and get the File content from it. It creates a list of filenames of files being uploaded and pass this list to success page.
Step3: Model – Form Object
Create a Java bean which acts as Model/Form object for our Spring application. This bean contains a List
of org.springframework.web.multipart.MultipartFile
objects. Spring framework provides a useful class MultipartFile which can be used to fetch the file content of uploaded file. Apart from its content, the MultipartFile object also gives you other useful information such as filename, file size etc.
Step4: JSP Views
Now create the view pages for this application. We will need two JSPs, one to display file upload form and another to show result on successful upload.
The uploadfile.jsp
displays a form with file input. Apart from this we have added small jquery snippetonclick of Add button. This will add a new file input component at the end of form. This allows user toupload as many files as they want.
Note that we have set enctype=”multipart/form-data”
attribute of our <form>
tag.
Step5: Update Spring Configuration
Add below bean to crunchify-servlet.xml
file, just above <beanid="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
line.
Step6: Checkout Result
Start tomcat and point your browser to this URL: http://localhost:8080/CrunchifySpringMVC3.2.1/upload.html and you should see screen similar tothis.
After file upload you will see success message like this. You can always beautify your .jsp file the wayyou want.
List of all Spring MVC Examples, Java Examples.
SHARE ON
TwitterFacebookGoogle+BufferPin ItFollow @CrunchifySome more articles you might also be interested in …
- Spring MVC: How to Declare a Bean in Spring Application?
- Simplest Spring MVC Hello World Example / Tutorial – Spring Model – View – Controller Tips
- How to use AJAX, jQuery in Spring Web MVC (.jsp) – Example
- Working on Spring MVC Project? How to Report List of All Loaded Spring Beans during Startup?
- How to Update Sparkline Graph Every 3 Seconds in Spring MVC (Realtime Update)
- WordPress: How to Save Time on Files Upload
- Spring MVC: Introduction to Spring 3 MVC Framework – Spring 4
- Spring MVC: How to Access ModelMap Values in a JSP?
- Read config.properties value using Spring ‘singleton’ Scope in your Java Enterprise Application
- How to Sort List of Files based on Last Modified Time in Ascending and Descending?
Enjoyed this post?
Be sure to subscribe to the Crunchify newsletter and get regular updates about awesomeposts just like this one and more! Join more than 13000 subscribers!
About Crunchify
Hello & Good Day from greater New York. Crunchify is founded by App Shah. He is a professional blogger & loves Web development hence Crunchify.com is his publication with more than 10 millions pageviews per month, that covers all aspects and tactics on Java, WordPress, J2EE (Enterprise Java), Spring MVC, Github, etc Technologies.
- Spring MVC 如何上传多个文件到指定位置
- 利用 Spring MVC 上传多文件到指定目录 spring upload files
- Spring MVC 多个文件上传
- java 多个文件上传 spring mvc
- Spring MVC多个文件上传
- JSP上传文件到指定位置
- winfrom上传多个文件到指定文件夹
- Nutz 上传文件之后进行文件搬运到指定位置
- spring mvc 多文件上传
- spring mvc 多文件上传
- spring MVC 多文件上传
- spring mvc 多文件上传
- Spring MVC 多文件上传
- Angular 4 上传多个文件到Spring boot
- ASP.Net中,如何自动保存文件到指定位置
- spring mvc 文件上传
- spring MVC 文件上传
- Spring MVC文件上传
- Java反射基础(二)--Fileds对象的使用
- 1011. A+B和C (15)
- Hexagon SDK知识学习
- 《数据库系统原理之ER模型》
- 徐汉彬:Web系统大规模并发——电商秒杀与抢购
- Spring MVC 如何上传多个文件到指定位置
- pch文件的创建与设置
- pga_aggregate_target和_pga_max_size都不能绝对限制实际PGA的使用
- hadoop 分布式计算开源框架 学习总结
- ios返回圆形图片
- AndroidStudio基础教程
- Android开发手记一 NDK编程实例
- [24]Swap Nodes in Pairs
- bzoj1072: [SCOI2007]排列perm