SpringMVC11文件上传
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<form action="user/login" method="post" enctype="multipart/form-data">
<input type="file" name="imgs"/>
<input type="file" name="imgs"/>
<input type="file" name="imgs"/>
<button>上传文件</button>
</form> </body>
</html>
index.jsp文件上传的页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>webroot success页面</h1>
</body>
</html>
success.jsp成功界面
@Controller
@RequestMapping("/user")
public class MyController {
/**
* 单个文件上传
* MultipartFile img :必须和表单中的name属性值一致
* @throws Exception
* @throws IllegalStateException @RequestMapping(value = "/login")
public String login(MultipartFile img, HttpSession session)
throws IllegalStateException, Exception {
System.out.println("进入了login......");
// 获取上传的位置
String path = session.getServletContext().getRealPath("/images");
// 获取文件的名称
String filename = img.getOriginalFilename();
System.out.println(img.getOriginalFilename());
System.out.println(img.getName()); // 表单中name的属性值
System.out.println(img.getContentType()); // 上传文件的类型
// 上传文件只能是图片 通过MIME类型 来判断
if (img.getContentType().equals("image/jpeg")) {
img.transferTo(new File(path, filename)); // 上传
} return "/success.jsp";
}*/ /**
* 多个文件上传
* @RequestParam 必须使用! 如果不加@RequestParam
* 默认前台的每一个imgs都是一个数组!
* 我们想要的是前台的所有上传文件 在一个数组里面!
*/
@RequestMapping(value = "/login")
public String login(@RequestParam MultipartFile[] imgs, HttpSession session)
throws IllegalStateException, Exception {
System.out.println("进入了login......");
// 获取上传的位置
String path = session.getServletContext().getRealPath("/images");
// 循环获取文件的名称
for (MultipartFile img : imgs) {
// 判断用户是否选择文件
if (img.getSize() > 0) {
String filename = img.getOriginalFilename();
img.transferTo(new File(path, filename)); // 上传
}
} return "/success.jsp";
} }
对应的controller代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 配置需要扫描的包 -->
<context:component-scan base-package="cn.bdqn"/>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!-- 配置文件上传 id必须是multipartResolver 因为已经在中央调度器
中定义了
Well-known name for the MultipartResolver object in the bean factory for this namespace. */
public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver";
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/><!--乱码问题 -->
<property name="maxUploadSize" value="1048576"/><!-- 文件总大小不能超过 1M-->
<!-- <property name="maxUploadSizePerFile" value="1048576"/>单个文件不能超过1m -->
</bean> </beans>
springmvc-servlet.xml文件
SpringMVC11文件上传的更多相关文章
- jquery.uploadify文件上传组件
1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
- Java FtpClient 实现文件上传服务
一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...
- 小兔Java教程 - 三分钟学会Java文件上传
今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...
- ,net core mvc 文件上传
工作用到文件上传的功能,在这个分享下 ~~ Controller: public class PictureController : Controller { private IHostingEnvi ...
- Web开发安全之文件上传安全
很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...
- AutoIt实现Webdriver自动化测试文件上传
在运用WebDriver进行自动化测试时,由于WebDriver自身的限制,对于上传文件时Windows弹出的文件选择窗口无法控制,通过在网上查找资料锁定使用AutoIt来控制文件上传窗口. Auto ...
- Struts的文件上传下载
Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...
- .JavaWeb文件上传和FileUpload组件使用
.JavaWeb文件上传 1.自定义上传 文件上传时的表单设计要符合文件提交的方式: 1.提交方式:post 2.表单中有文件上传的表单项:<input type="file" ...
随机推荐
- 转:基于HTTP协议的轻量级开源简单队列服务:HTTPSQS
[文章作者:张宴 本文版本:v1.7.1 最后修改:2011.11.04 转载请注明原文链接:http://blog.zyan.cc/httpsqs/] HTTPSQS(HTTP Simple Que ...
- wlan的QOS配置
WLAN QoS配置 1.1 WLAN QoS简介 802.11网络提供了基于竞争的无线接入服务,但是不同的应用需求对于网络的要求是不同的,而原始的网络不能为不同的应用提供不同质量的接入服务,所以已 ...
- [转]加盐hash保存密码的正确方式
0x00 背景 大多数的web开发者都会遇到设计用户账号系统的需求.账号系统最重要的一个方面就是如何保护用户的密码.一些大公司的用户数据库泄露事件也时有发生,所以我们必须采取一些措施来保护用户的密码, ...
- [BZOJ 1026] [SCOI 2009] Windy数 【数位DP】
题目链接:BZOJ - 1026 题目分析 这道题是一道数位DP的基础题,对于完全不会数位DP的我来说也是难题.. 对于询问 [a,b] 的区间的答案,我们对询问进行差分,求 [0,b] - [0,a ...
- [BZOJ 3747] [POI 2015] Kinoman【线段树】
Problem Link : BZOJ 3747 题解:ZYF-ZYF 神犇的题解 解题的大致思路是,当区间的右端点向右移动一格时,只有两个区间的左端点对应的答案发生了变化. 从 f[i] + 1 到 ...
- 1027. Colors in Mars (20) PAT
题目:http://pat.zju.edu.cn/contests/pat-a-practise/1027 简单题,考察十进制数和n进制数的转换和输出格式的控制. People in Mars rep ...
- zabbix 默认item采集使用被动模式 需要改为主动模式
数据采集的工作模式可以分为被动模式(服务器端到客户端采集数据) 主动模式(客户端主动上报数据到服务器端) 服务器配置: DBHost=192.168.32.55 DBName= zabbix DBUs ...
- cognos启动报错
[ ERROR ] Content Manager is unable to process your request because an unexpected event occurred in ...
- java实战之数组工具集
java是一门面向对象的语言,我们也提到过,面向对象的一个优点就在于能够提高代码的复用性,前面我们详细讲过数组的查找,排序,等等,为了提高代码的复用性,我们何不自己写一个数组的工具集,来综合下前面所学 ...
- Instant Complexity(模拟,递归)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1535 Accepted: 529 Description Analyz ...