1.创建上传页面

<form action="first.do" method="post"
enctype="multipart/form-data">
<input type="file" name="uploadFile" value="选择文件" /> <input type="submit"
value="提交" />
</form>

2.配置控制器

@Controller
public class MyController { // 处理器方法
@RequestMapping(value = "/first.do")
public String doFirst(HttpSession session, MultipartFile uploadFile)
throws Exception { // 1.获取文件名称
String originalFilename = uploadFile.getOriginalFilename();
// 6.必须选择上传文件
if (uploadFile.getSize() > 0) {
// 5.限制文件类型
if (originalFilename.endsWith("jpg")
|| originalFilename.endsWith("png")
|| originalFilename.endsWith("txt")) {
// 2.获取保存的前路径
String servletContext = session.getServletContext()
.getRealPath("upload");
System.out.println(servletContext);
// 3.拼接路径
File realPath = new File(servletContext, originalFilename);
// 4.保存
uploadFile.transferTo(realPath);
return "/two.jsp";
}
}
return "/error.jsp";
}
}

3.配置applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 扫描包中注解标注的类 -->
<context:component-scan base-package="cn.cnsdhzzl.controller" /> <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置编码 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- 设置上传文件总大小 -->
<property name="maxUploadSize" value="5000" />
<!-- 设置单个上传文件大小 -->
<property name="maxUploadSizePerFile" value="1000" />
</bean> <mvc:annotation-driven /> </beans>

springmvc单文件上传的更多相关文章

  1. SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...

  2. [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载

    一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...

  3. springMVC数据校验与单文件上传

    spring表单标签:    <fr:from/> 渲染表单元素    <fr:input/>输入框组件    <fr:password/>密码框组件标签    & ...

  4. springmvc实现文件上传

    springmvc实现文件上传 多数文件上传都是通过表单形式提交给后台服务器的,因此,要实现文件上传功能,就需要提供一个文件上传的表单,而该表单就要满足以下3个条件 (1)form表彰的method属 ...

  5. SSM框架-SpringMVC 实例文件上传下载

    一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...

  6. 【SpringMVC】文件上传Expected MultipartHttpServletRequest: is a MultipartResolver错误解决

    本文转载自:https://blog.csdn.net/lzgs_4/article/details/50465617 使用SpringMVC实现文件上传时,后台使用了 MultipartFile类, ...

  7. springMvc之文件上传与下载

    我们经常会使用的一个功能是文件下载,既然有文件下载就会有文件上传,下面我们来看一下文件上传是如何实现的 首先准备好一个页面 <style type="text/css"> ...

  8. 关于SpringMVC的文件上传

    关于文件的上传,之前写过2篇文章,基于Struts2框架,下面给出文章链接: <关于Struts2的文件上传>:http://www.cnblogs.com/lichenwei/p/392 ...

  9. jsp+springmvc实现文件上传、图片上传和及时预览图片

    1.多文件上传:http://blog.csdn.net/a1314517love/article/details/24183273 2.单文件上传的简单示例:http://blog.csdn.net ...

随机推荐

  1. JS对象之间的关系

    JS对象类型 JS中,可以将对象分为"内部对象"."宿主对象"和"自定义对象"三种. 1.本地对象 ECMA-262定义为"独立于 ...

  2. HTTPS强制安全策略-HSTS协议阅读理解

    https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security [阅读理解式翻译,非严格遵循原 ...

  3. jq选中问题

    var objs = $("div[id^='offer_details_']"); //遍历点击事件监听问题 onclick="details("+offer ...

  4. Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program

    开启tomcat时出现以上错误 // 进入root帐户 在普通用户也可以 sudo -s //用编辑器打开.bashrc文件 gedit .bashrc 在普通用户下注意加上sudo //在最后一行添 ...

  5. YII2.0--------这篇文章记录我学习YII2.0的过程吧,也可以让更多的人少走弯路

    1.情况:今天我从github上下载了一个项目,本以为直接丢到根目录运行就行了,但是不行. 解决办法:首先安装git,安装步骤这里不讲了,稍微讲一下配置环境变量.

  6. Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

    有3个对象,对象A,对象B,对象C.他们的实体关系为: 1.A中存在List<B>和List<C>,即一个包含另外两个: 2.A中存在List<B>,B中存在Lis ...

  7. 制作圆角:《CSS3 Border-radius》

    今天我们在一起来看看CSS3中制作圆角的属性border-radius的具体用法.如今CSS3中的border-radius出现后,让我们没有那么多的烦恼了,首先制作圆角图片的时间是省了,而且其还有多 ...

  8. NSRuntime RunLoop

    Objective-C总Runtime的那点事儿(一)消息机制 http://www.cocoachina.com/ios/20141018/9960.html Runtime运行时的那点事儿 htt ...

  9. Lucas定理

    Lucas' theorem In number theory, Lucas's theorem expresses the remainder of division of the binomial ...

  10. js给文本框赋值 value与innerHTML

    <input type="test" name="testName" id="testId"> 赋值操作: <script ...