springmvc文件上传
来源:互联网 发布:阿里云邮件推送教程 编辑:程序博客网 时间:2024/06/05 17:25
注意点 :
- 需要配置
multipartResolver
解析器进行文件上传 - springmvc默认使用commons-fileupload上传文件,需要引入commons-fileupload 和 commons-io 两个包
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
spring-mvc.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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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="com.yc.us.web" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> <!-- 给视图加上后缀 如:视图是success,返回是success.jsp --> </bean> <mvc:default-servlet-handler /> <mvc:annotation-driven /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸1024字节=1K --> <property name="maxUploadSize"> <value>1024000</value> </property> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean></beans>
Handler
@RequestMapping(path="/modify",method=RequestMethod.POST) @ResponseBody public boolean modify(@RequestParam(name="pic",required=false)MultipartFile file,User user,HttpServletRequest request){ System.out.println(file); if(file!=null&&!file.isEmpty()){ String path = new File(request.getSession().getServletContext().getRealPath("/")).getParent()+"\\upload\\"+file.getOriginalFilename(); System.out.println(path); File saveTargetFile = new File(path); if(!saveTargetFile.exists()){ try { saveTargetFile.createNewFile(); } catch (IOException e) { return false; } } //保存 try { file.transferTo(saveTargetFile); } catch (IllegalStateException | IOException e) { return false; } } System.out.println(user); return true; }
Form
<form id="modifyForm" method="post" enctype="multipart/form-data"> <p><input id="uid" name="id" readonly="readonly"></p> <p><input id="uname" name="name"></p> <p><input id="ubirthday" name="birthday"></p> <p><input id="ugender" name="gender"></p> <p><input id="ucareer" name="career"></p> <p><input id="uaddress" name="address"></p> <p><input id="umobile" name="mobile"></p> <p><input type="file" id="upic" name="pic" onChange='changePic(this)'><br> <img src="image/not_pic.jpg" id=pic> </p> <p><a class='closebtn' href='javascript:void(0)'>关闭</a> <a class='updatebtn' href='javascript:void(0)'>修改</a> </p> </form>
0 0
- springMVC(11) springMVC文件上传
- SpringMVC-----8、SpringMVC上传文件
- springmvc文件上传总结
- SpringMVC multipart文件上传
- SpringMVC中的文件上传
- springmvc的文件上传
- springMVC上传文件
- springMVC 文件上传
- SpringMVC - 文件上传篇
- springMVC文件上传
- springMVC文件上传
- springMVC文件上传
- SpringMVC 上传文件
- SpringMVC文件上传
- springMVC上传文件
- springMVC上传文件例子
- springMVC上传文件
- springmvc文件上传错误
- stm32固件库assert_param()
- 每日一则(2):call apply bind
- 【PAT】PAT-JAVA-5-2 然后是几点 (15分)
- CSS3新特性
- 第一篇博客
- springmvc文件上传
- 子查询
- String、StringBuffer和StringBuilder的区别以及使用方法
- iOS 开发 深入浅出Runtime运行时之官方指南翻译--消息发送
- 互斥及其应用
- 烟大 1942 进制转换任意转换
- NOIP2016滚粗~~~~
- Python中With的用法
- 多线程java懒加载问题