MultipartResolver实现文件上传功能
转自:https://www.jb51.net/article/142736.htm
springMVC默认的解析器里面是没有加入对文件上传的解析的,,使用springmvc对文件上传的解析器来处理文件上传的时需要用springmvc提供的MultipartResolver的申明,又因为CommonsMultipartResolver实现了MultipartResolver接口,所以我们可以在springmvc配置文件中这样配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>
首先引入文件上传所需要的包,commons-logging-*.jar commons-io-*.jar commons-fileupload-*.jar
新建一个JSP页面.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<%--<form action="user/fileUpload" method="post" enctype="multipart/form-data">--%>
<form action="user/fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload" />
<input type="submit" value="上传" />
</form>
</body>
</html>
springmvc上传文件的形式有很多,这里我介绍两种.
第一种,看Controller
package gd.hz.springmvc.controller; import java.io.File;
import java.io.IOException; 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.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView; @Controller("userController")
@RequestMapping("user")
public class UserController { // 处理文件上传一
@RequestMapping(value = "fileUpload", method = RequestMethod.POST)
public ModelAndView fileUpload(
@RequestParam("fileUpload") CommonsMultipartFile file) {
// 获取文件类型
System.out.println(file.getContentType());
// 获取文件大小
System.out.println(file.getSize());
// 获取文件名称
System.out.println(file.getOriginalFilename()); // 判断文件是否存在
if (!file.isEmpty()) {
String path = "D:/" + file.getOriginalFilename();
File localFile = new File(path);
try {
file.transferTo(localFile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return new ModelAndView("dataSuccess");
}
}
类CommonsMultipartFile为我们提供了许多对文件处理的方法.例如文件大小,上传文件名称,文件类型,具体用法可以查看spring的文档.transferTo就是将文件输出到指定地方.
文件上传的第二种方法,这种方法比较常用:
package gd.hz.springmvc.controller; import java.io.File;
import java.io.IOException;
import java.util.Iterator; 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.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver; @Controller("userController")
@RequestMapping("user")
public class UserController { // 处理文件上传二
@RequestMapping(value = "fileUpload2", method = RequestMethod.POST)
public String fileUpload2(HttpServletRequest request)
throws IllegalStateException, IOException {
// 设置上下方文
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext()); // 检查form是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) { // 由CommonsMultipartFile继承而来,拥有上面的方法.
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {
String fileName = "demoUpload" + file.getOriginalFilename();
String path = "D:/" + fileName; File localFile = new File(path);
file.transferTo(localFile);
} }
}
return "dataSuccess";
}
}
MultipartHttpServletRequest提供了更加灵活的方法,可以获取多个文件和文件名,可以遍历获得每个文件.
MultipartResolver实现文件上传功能的更多相关文章
- Spring 文件上传功能
本篇文章,我们要来做一个Spring的文件上传功能: 1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖: <dependency> <groupId> ...
- Spring +SpringMVC 实现文件上传功能。。。
要实现Spring +SpringMVC 实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...
- spring mvc 3.0 实现文件上传功能
http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...
- springmvc中使用文件上传功能
项目代码:https://github.com/PeiranZhang/springmvc-fileupload Servlet3.0之前使用文件上传功能 Servlet3.0之前需要使用common ...
- PHPCMS_V9 模型字段添加单文件上传功能
后台有“多文件上传”功能,但是对于有些情况,我们只需要上传一个文件,而使用多文件上传功能上传一个文件,而调用时调用一个文件URL太麻烦了. 使用说明: 1.打开phpcms\modules\conte ...
- 配置php.ini实现PHP文件上传功能
本文介绍了如何配置php.ini实现PHP文件上传功能.其中涉及到php.ini配置文件中的upload_tmp_dir.upload_max_filesize.post_max_size等选项,这些 ...
- MVC5:使用Ajax和HTML5实现文件上传功能
引言 在实际编程中,经常遇到实现文件上传并显示上传进度的功能,基于此目的,本文就为大家介绍不使用flash 或任何上传文件的插件来实现带有进度显示的文件上传功能. 基本功能:实现带有进度条的文件上传功 ...
- 用c++开发基于tcp协议的文件上传功能
用c++开发基于tcp协议的文件上传功能 2005我正在一家游戏公司做程序员,当时一直在看<Windows网络编程> 这本书,把里面提到的每种IO模型都试了一次,强烈推荐学习网络编程的同学 ...
- Node.js新手教程——怎样实现文件上传功能
作者:zhanhailiang 日期:2014-11-16 本文将介绍怎样使用Node.js实现文件上传功能. 1. 初始化项目信息:npm init [root@~/wade/nodejs/node ...
随机推荐
- HDU 2352 Verdis Quo
罗马数字转化为十进制的值 题目非常的长 提取有效信息 并且介绍很多规则 但是事实上有用的信息就是如何加 什么时候减 当当前字母小于下一个字母时 减去当前字母的值 #include <iostre ...
- SystemInformationRequestHandlers
SystemInformationRequestHandlers - Solr Wiki Search: Solr Wiki Login SystemInformationRequestHandler ...
- [Noip复习知识点][个人向]Zackzh
只是列列一些要复习的,努力复习吧,有种noip退役的赶脚. 一.模拟 (这你也不会?退役吧) 二.DP 1.基础dp 2.区间dp 3.状压dp 4.树形dp 6.概率(期望)dp 7.环形dp 8. ...
- HDU 1114 【DP】
题意: 给你空钱袋的质量和装满钱的钱袋的质量. 给你先行的n种货币的面值和质量. 问钱包里的钱最少是多少. 如果质量不可行,输出impossible. 思路: 完全背包. 屌丝有个地方没想通,就是如何 ...
- 关于Java第一次实验的对课后问题自己的理解--验证码实现及其四则运算
问题一.对于课上ppt中EnumTest所提出的的问题进行解答 将这段代码放到文件中进行运行后发现 1.对应的Size中不同元素的并不是同一个对象 2.以其中一个枚举类型s来说,不是原始数据,即他们都 ...
- ntfs格式uefi启动u盘
http://www.laomaotao.org/softhelp/syjc/925.html http://www.laomaotao.org/softhelp/wtjd/989.html http ...
- S5700&S5710 产品文档 : 配置
http://support.huawei.com/hdx/hdx.do?docid=SC0000699332&lang=zh&path=PBI1-C103367%2FPBI1-C10 ...
- 机器学习技法总结(五)Adaptive Boosting, AdaBoost-Stump,决策树
上一讲主要利用不同模型计算出来的g.採用aggregation来实现更好的g.假设还没有做出来g.我们能够採用bootstrap的方法来做出一系列的"diversity"的data ...
- Android 怎样查看系统的memory swap 资讯/信息
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command
在新建cocos2d-x以后,执行发现下面错误: ld: -pie can only be used when targeting iOS 4.2 or later clang: error: lin ...