SpringMVC文件上传实现
SpringMVC(注解)上传文件需要注意的几个地方:
1、form的enctype="multipart/form-data",这个是上传文件必须的
2、applicationContext.xml配置:
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="200000"/>
<!-- 最大内存大小 (10240)-->
<property name="maxInMemorySize" value="40960" />
</bean> <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
<!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
</props>
</property>
</bean>
用于上传的表单页面/WEB-INF/jsp/upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传图片</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/upload/filesUpload" method="POST" enctype="multipart/form-data">
yourfile: <input type="file" name="myfiles"/><br/>
yourfile: <input type="file" name="myfiles"/><br/>
<input type="submit" value="上传图片"/>
</form>
</body>
</html>
上传文件内容过大时的提示页面/WEB-INF/jsp/error_fileupload.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<h1>文件过大,请重新选择</h1>
上传文件的核心UploadController类
package com.ljq.web.controller.annotation; import java.io.File; import javax.servlet.http.HttpServletRequest; 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; /**
* 上传图片
*
* @author Administrator
*
*/
@Controller
@RequestMapping("/upload")
public class UploadController { @RequestMapping("/toUpload")
public String toUpload() {
return "/upload";
} /***
* 保存文件
*
* @param file
* @return
*/
private boolean saveFile(HttpServletRequest request, MultipartFile file) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 保存的文件路径(如果用的是Tomcat服务器,文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\upload\\文件夹中 )
String filePath = request.getSession().getServletContext()
.getRealPath("/") + "upload/" + file.getOriginalFilename();
File saveDir = new File(filePath);
if (!saveDir.getParentFile().exists())
saveDir.getParentFile().mkdirs(); // 转存文件
file.transferTo(saveDir);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
} /**
* 上传图片
*
* @param files
* @param request
* @return
*/
@RequestMapping("/filesUpload")
public String filesUpload(@RequestParam("myfiles") MultipartFile[] files,
HttpServletRequest request) {
if (files != null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
MultipartFile file = files[i];
// 保存文件
saveFile(request, file);
}
} // 重定向
return "redirect:/upload/toUpload";
} }
到此文件上传开发就结束了。
MultipartFile类常用的一些方法:
String getContentType() //获取文件MIME类型
InputStream getInputStream() //返回文件流
String getName() //获取表单中文件组件的名字
String getOriginalFilename() //获取上传文件的原名
long getSize() //获取文件的字节大小,单位byte
boolean isEmpty() //是否为空
void transferTo(File dest) //保存到一个目标文件中
SpringMVC文件上传实现的更多相关文章
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- springmvc文件上传下载简单实现案例(ssm框架使用)
springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...
- springMVC文件上传大小超过限制的问题
[转自]https://my.oschina.net/ironwill/blog/646762 springMVC是一个非常方便的web层框架,我们使用它的文件上传也非常的方便. 我们通过下面的配置来 ...
- 解决springMVC文件上传报错: The current request is not a multipart request
转自:https://blog.csdn.net/HaHa_Sir/article/details/79131607 解决springMVC文件上传报错: The current request is ...
- 18 SpringMVC 文件上传和异常处理
1.文件上传的必要前提 (1)form 表单的 enctype 取值必须是:multipart/form-data(默认值是:application/x-www-form-urlencoded) en ...
- springmvc文件上传AND jwt身份验证
SpringMVC文件上传 思路:1.首先定义页面,定义多功能表单(enctype=“multipart/form-data”)2.在Controller里面定义一个方法,用参数(MultipartF ...
- TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式
1.传统文件上传方式 <!-- 文件上传需要的jar --> <dependency> <groupId>commons-fileupload</groupI ...
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
- springmvc 文件上传实现(不是服务器的)
1.spring使用了apache-commons下的上传组件,因此,我们需要引用2个jar包 1)apache-commons-fileupload.jar 2 ) apache-commons-i ...
- SpringMVC文件上传和下载
上传与下载 1文件上传 1.1加入jar包 文件上传需要依赖的jar包 1.2配置部件解析器 解析二进制流数据. <?xml version="1.0" encoding=& ...
随机推荐
- 表格制作模块xlwt
import xlwtworkbook = xlwt.Workbook(encoding = 'ascii') #创建workbook 括号内容视情况而定sheetname = 'Sheet'book ...
- C#WebClient常见用法
System.Net.WebClient.DownloadFile(Uri address, String fileName) namespace:System.Net 参数: address:The ...
- 揭开HTTP网络协议神秘面纱系列(二)
HTTP报文内的HTTP信息 HTTP协议交互的信息被称为HTTP报文,请求端的HTTP报文叫做请求报文,响应端的叫做响应报文. HTTP为了提升传输速率,其在传输数据时,按照数据原样进行压缩传输,相 ...
- RecycleView在eclipse的初体验
在sdk中找到v7包 \sdk\extras\android\support\v7\recyclerview 导入工程 Import\Android\Existing Android Code Int ...
- 怎样把windows中安装的程序列出来?
症状/问题我怎样把windows中安装的程序信息输出到一个文本文件中?解决方法使用 windows 操作系统中的命令:wmic就可以做到.下面的命令就可以把系统中安装的程序都输出到文件ProgramL ...
- ASP.NET 获取不同frame中的控件
最近在做网站时遇到一个问题,需要获取不同frame中的控件,请教了一些同事,他们都说是无法取到的, 在网上查找了很多资料,找到了解决方法,现在整理一下当初的解决思路: 1.我需要在mainFrame中 ...
- Oracle列操作(增加列,修改列,删除列)
Oracle列操作 增加一列: alter table emp4 add test varchar2(10); 修改一列: alter table emp4 modify test varchar2( ...
- 关于java中线程休眠的另一种写法
编辑器加载中... 优先使用TimeUnit类中的sleep() TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,TimeUnit提供了可读性更好的 ...
- ubuntu 14.04 编译内核出现unable to locate package ncurses-devel 问题的解决
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422461614 ...
- VB6.0和VB.Net的函数等对照表
VB6.0和VB.Net的对照表 VB6.0 VB.NET AddItem Object名.AddItem Object名.Items.Add ListBox1.Items.Add ComboBox1 ...