第一步:导入commons-fileupload-1.3.1.jar 和commons-io-2.2.jar 架包

第二步:在applicationContext.xml中 配置

<bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/>

第三步:在页面 <form>标签添加属性enctype=”multipart/form-data” 这个是上传文件必须的

具体一个简单方法如下

1.web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 配置pringmvc前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 设置springmvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/shopping-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

2.shopping-servlet.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:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 让spring容器自动探知bean Controller Service Respoise Componet -->
<!--
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
--> <context:component-scan base-package="com.bdqnsjz.shopping.controller"></context:component-scan> <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="34564356345456" p:maxUploadSizePerFile="3456435634545" p:defaultEncoding="utf-8">
<!--
<property name="defaultEncoding" value="utf-8"></property>
-->
</bean> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

3.controller

 @RequestMapping(value="/upload1", method=RequestMethod.POST)
public String upload1(@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request){ String fileName = file.getOriginalFilename();
System.out.println(fileName); //获取扩展名
String extension = fileName.substring(fileName.lastIndexOf(".")); //新的文件名 234-135234532l34kjlkj.jpg
String savedFileName = java.util.UUID.randomUUID().toString() + extension; String folderPath = request.getRealPath("/WEB-INF/upload"); File savedFile = new File(folderPath, savedFileName); //保存文件
try {
file.transferTo(savedFile);
} catch (IOException e) {
e.printStackTrace();
} return "upload1";
}

SpringMvc (注解)中的上传文件的更多相关文章

  1. SpringMVC实现PUT请求上传文件

    在JQuery中,我们可以进行REST ful中delete和put的请求,但是在java EE标准中,默认只有在POST请求的时候,servlet 才会通过getparameter()方法取得请求体 ...

  2. Android应用开发中webview上传文件的几种思路

    1. 常规方法,重写WebChromeClient 的 openFileChooser 方法 private class MyWebChromeClient extends WebChromeClie ...

  3. (H5)FormData+AJAX+SpringMVC跨域异步上传文件

    最近都没时间整理资料了,一入职就要弄懂业务,整天被业务弄得血崩. 总结下今天弄了一个早上的跨域异步上传文件.主要用到技术有HTML5的FormData,AJAX,Spring MVC. 首先看下上传页 ...

  4. Juploader 1.0 谷歌(chrome)浏览器中成功上传文件后返回信息异常

    在项目中使用了Juploader 1.0无刷新上传文件的js组件,在IE8以上没有问题,代码如下: function InitialUploadDirectly(OnUploadFunc, butto ...

  5. 定制FileField中的上传文件名称

    FileField中的upload_to属性可以设定上传文件的存储目录和名称,它可以是个字符串,也可以是个callable,比如一个方法. 当upload_to的值设为一个方法时,就可以对上传文件的名 ...

  6. ASP.Net在web.config中设置上传文件的大小方法

    修改Webcong文件:<system.web><httpRuntime maxRequestLength="40960"   //即40MB,1KB=1024u ...

  7. 在asp.net 中怎样上传文件夹

    以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  ...

  8. Element UI中的上传文件功能

    上传文件给后台: <el-upload style="display:inline-block" :limit=" class="upload-demo& ...

  9. 【Azure 应用服务】PHP应用部署在App Service for Linux环境中,上传文件大于1MB时,遇见了413 Request Entity Too Large 错误的解决方法

    问题描述 在PHP项目部署在App Service后,上传文件如果大于1MB就会遇见 413 Request Entity Too Large 的问题. 问题解决 目前这个问题,首先需要分析应用所在的 ...

随机推荐

  1. firefox PAC代理

    在linux用firefox,又不像windows下的chrome那样,可以直接设置个自动代理,但是发现firefox有个自动代理的功能哦. /home/allen/Documents/google. ...

  2. 1 基于梯度的攻击——FGSM

    FGSM原论文地址:https://arxiv.org/abs/1412.6572 1.FGSM的原理 FGSM的全称是Fast Gradient Sign Method(快速梯度下降法),在白盒环境 ...

  3. C语言|作业07

    一.本周作业头 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://i-beta.cnblogs.com/posts/edit;postId=11811545 我在这个课程的 ...

  4. mysql的root密码忘了

    忘记密码:https://www.cnblogs.com/ryanzheng/p/9348723.htmlmy.cnf的[mysqld]下加:skip-grant-tables重启 mysql -ur ...

  5. Xtrabackup innobackupex

    Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工. Xtrabackup中主要包含两个工具 ...

  6. C++学习 之 函数的重载及内联(笔记)

    1.函数的使用: 1.1 将数组传递给函数: 当需要给函数传递数组作为参数时,其实传过来的是实参的地址就相当于使用引用或指针作为形参. 例: int DisPlayArray(int Number[] ...

  7. 打印指针要用%p而不要用%x

    注意: 打印指针要用%p而不要用%x 原因: https://boredzo.org/blog/archives/2007-01-23/please-do-not-use-percent-x-for- ...

  8. JS实现把一个页面层数据传递到另一个页面的两种方式

    本博客整理了两种方式从一个页面层向另一个页面层传递参数. 一. 通过cookie方式 1. 传递cookie页面的html,此处命名为a.html 请输入用户名和密码: <input id=&q ...

  9. python-1:正则表达式(基础知识点)

    1.简单匹配: \d  →匹配一个数字 \w →匹配一个数字或字母 \s  →匹配一个空格(包括tab等空白符) .    →匹配任意字符 *   →匹配任意个字符(包括0个) +  →匹配至少一个字 ...

  10. 使用Python基于VGG/CTPN/CRNN的自然场景文字方向检测/区域检测/不定长OCR识别

    GitHub:https://github.com/pengcao/chinese_ocr https://github.com/xiaofengShi/CHINESE-OCR |-angle 基于V ...