SSM-SpringMVC-32:SpringMVC中灌顶传授文件上传
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
我将用自认为最简单的语言,描述Springmvc的文件上传,来将老夫毕生功力灌顶传授给你
首先文件上传,又简至深
前提有吗?jar包,form表单里的属性(method="post" enctype="multipart/form-data")
jar包的节点我给出来:
<!--文件上传的jar包--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency>
下面我开始我第一个案例,最简单的文件上传:
1.jsp页面:fileupload.jsp
<%-- Created by IntelliJ IDEA. User: Dawn Date: 2018/4/2 Time: 14:19 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>文件上传</title> </head> <body> <h1>文件上传</h1> <form action="${pageContext.request.contextPath}/fileuploadfirst" method="post" enctype="multipart/form-data"> 文件1 <input type="file" name="upload"/> <input type="submit"/> </form> </body> </html>
success.jsp
<%-- Created by IntelliJ IDEA. User: Dawn Date: 2018/4/2 Time: 14:19 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>成功</title> </head> <body> <h1>SUCCESS</h1> </body> </html>
2.在webapp下你jsp页面的那个包下创建一个文件夹,我的叫upload,里面随便扔个东西,编译后,如果即使这样target目录下还没有upload这个文件夹的话,就手动创建
3.创建处理器和处理方法
package cn.dawn.day24fileupload; import com.sun.org.glassfish.gmbal.ParameterNames; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; /** * Created by Dawn on 2018/4/2. */ @Controller public class FileupLoad { /*最初始版本*/ @RequestMapping("/fileuploadfirst") public String fileuploadfirst(MultipartFile upload, HttpSession session){ /*获取上传文件的简单名称例如 1.txt*/ String childrlPath = upload.getOriginalFilename(); /*获得一个真实路径*/ String parentPath = session.getServletContext().getRealPath("/day24/upload"); /*获取一个完整的文件对象*/ File file=new File(parentPath,childrlPath); /*传输创建到本地*/ try { upload.transferTo(file); /*上传成功*/ return "success"; } catch (IOException e) { e.printStackTrace(); } /*上传失败*/ return "fileupload"; } }
4.自己的xml配置文件:这儿其实我想删减点的,文件名中文的处理和文件大小限制放在后面讲也行,不过放在这儿,你们应该也能理解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--包扫描器--> <context:component-scan base-package="cn.dawn.day24fileupload"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/day24/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--多部分文件解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--文件名的编码--> <property name="defaultEncoding" value="UTF-8"></property> <!--限制文件大小--><!--这个单位是byte,我这儿限制的是20mb最大容量--> <property name="maxUploadSize" value="20971520"></property> </bean> <!--绑定注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
5.web.xml中修改中央处理器的上下文配置参数为上面那个xml
6.启动tomcat,访问fileupload.jsp页面
第二个案例:多文件上传
1.jsp页面fileuploadmore.jsp:
<%-- Created by IntelliJ IDEA. User: Dawn Date: 2018/4/2 Time: 14:19 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>文件上传</title> </head> <body> <h1>文件上传</h1> <form action="${pageContext.request.contextPath}/fileuploadsecond" method="post" enctype="multipart/form-data"> 文件1 <input type="file" name="upload"/> 文件2 <input type="file" name="upload"/> 文件3 <input type="file" name="upload"/> <input type="submit"/> </form> </body> </html>
success.jsp
<%-- Created by IntelliJ IDEA. User: Dawn Date: 2018/4/2 Time: 14:19 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>成功</title> </head> <body> <h1>SUCCESS</h1> </body> </html>
2.处理器处理方法
package cn.dawn.day24fileupload; import com.sun.org.glassfish.gmbal.ParameterNames; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; /** * Created by Dawn on 2018/4/2. */ @Controller public class FileupLoad { /*多文件版本*/ @RequestMapping("/fileuploadsecond") public String fileuploadsecond(@RequestParam MultipartFile[] upload, HttpSession session){ for (MultipartFile item :upload) { if(item.getSize()>0) { /*获取上传文件的简单名称例如 1.txt*/ String childrlPath = item.getOriginalFilename(); /*获得一个真实路径*/ String parentPath = session.getServletContext().getRealPath("/day24/upload"); /*获取一个完整的文件对象*/ File file = new File(parentPath, childrlPath); /*传输创建到本地*/ try { item.transferTo(file); /*上传成功*/ } catch (IOException e) { e.printStackTrace(); return "fileuploadmore"; } } } /*上传失败*/ return "success"; } /*最初始版本*/ @RequestMapping("/fileuploadfirst") public String fileuploadfirst(MultipartFile upload, HttpSession session){ /*获取上传文件的简单名称例如 1.txt*/ String childrlPath = upload.getOriginalFilename(); /*获得一个真实路径*/ String parentPath = session.getServletContext().getRealPath("/day24/upload"); /*获取一个完整的文件对象*/ File file=new File(parentPath,childrlPath); /*传输创建到本地*/ try { upload.transferTo(file); /*上传成功*/ return "success"; } catch (IOException e) { e.printStackTrace(); } /*上传失败*/ return "fileupload"; } }
3.自己的xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--包扫描器--> <context:component-scan base-package="cn.dawn.day24fileupload"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/day24/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--多部分文件解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--文件名的编码--> <property name="defaultEncoding" value="UTF-8"></property> <!--限制文件大小--><!--这个单位是byte,我这儿限制的是20mb最大容量--> <property name="maxUploadSize" value="20971520"></property> </bean> <!--绑定注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
4.修改web.xml的中央调度器的上下文配置位置为上面那个xml
5.在webapp下你jsp页面的那个包下创建一个文件夹,我的叫upload,里面随便扔个东西,编译后,如果即使这样target目录下还没有upload这个文件夹的话,就手动创建
6.启动tomcat,访问fileuploadmore.jsp页面
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
mac 终端运行py文件
试验程序为:通过影藏后缀名,取得指定目录下的指定文件 import osimport sys startdir = '/Users/./'for dirpath,dirnames,filenames in os.walk(startdir):for filename in filenames:if os.path.splitext(filename)[1] == '.docx': filepath = os.path.join(dirpath,filename)print('file:' + filepath) 打开MAC终端,输入: python /Users/./1.py
- 下一篇
Java爬虫之利用Jsoup自制简单的搜索引擎
Jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。Jsoup的官方网址为: https://jsoup.org/, 其API使用手册网址为:https://jsoup.org/apidocs/overview-summary.html . 本次分享将实现的功能为:利用Jsoup爬取某个搜索词语(暂仅限英文)的百度百科的介绍部分,具体的功能介绍可以参考博客:Python爬虫——自制简单的搜索引擎。在上篇爬虫中我们用Python进行爬取,这次,我们将用Java来做爬虫,你没看错,就是Java. 在Eclipse中加入Jsoup包,下载网址为:https://jsoup.org/download . 爬虫的具体代码如下: package baiduScrape; /* * 本爬虫主要利用Java的Jsoup包进行网络爬取 * 本爬虫的功能: 爬取百度百科的开头介绍部分 * 使用方法: 输入关键字(目前只支持英文)即可 */ import ...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL8.0.19开启GTID主从同步CentOS8
- Linux系统CentOS6、CentOS7手动修改IP地址
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合Redis,开启缓存,提高访问速度
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作