springmvc 怎么让页面file类型的值 与后台映射
2个回答
推荐于2017-12-16
展开全部
SpringMVC默认是关闭fileupload功能的,开启该能够并验证文件上传,需要做如下几件事情:
第一:打开SpringMVC的文件上传功能:
***-servlet.xml中配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="10240" />
</bean>
配置后,当SpringMVC接受到multipartRequest的时候,就会把HttpServletRequest转为MultipartHttpServletRequest类型,
第二步:创建上传文件的file.jsp:
<form action="dynamicFields.action?method=uploadFile" method="post" enctype="multipart/form-data"><input type="file" name="myfile" id="myfile" value="" /><br/><input type="submit" value="确认提交"></form>
一定不要漏掉enctype="multipart/form-data",否则web容器认为这不是一个MultipartRequest请求,会报错org.springframework.web.multipart.MultipartException: The current request is not a multipart request。
第三步:Controller层创建代码:
@RequestMapping(params = {"method=uploadFile"})
public ModelAndView uploadFile(@RequestParam("myfile") MultipartFile myfile,
HttpServletRequest request, HttpServletResponse response) throws Exception {
if(!myfile.isEmpty()){
logger.info(myfile.getName());
byte[] bs= myfile.getBytes() ;
logger.info(new String(bs));
}
return null;
}
部署web应用运行后,能够看到控制台中打印出上传文件的内容,BINGO搞定(SpringMVC依赖common-fileupload.jar,需要加载该jar包)
第一:打开SpringMVC的文件上传功能:
***-servlet.xml中配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="10240" />
</bean>
配置后,当SpringMVC接受到multipartRequest的时候,就会把HttpServletRequest转为MultipartHttpServletRequest类型,
第二步:创建上传文件的file.jsp:
<form action="dynamicFields.action?method=uploadFile" method="post" enctype="multipart/form-data"><input type="file" name="myfile" id="myfile" value="" /><br/><input type="submit" value="确认提交"></form>
一定不要漏掉enctype="multipart/form-data",否则web容器认为这不是一个MultipartRequest请求,会报错org.springframework.web.multipart.MultipartException: The current request is not a multipart request。
第三步:Controller层创建代码:
@RequestMapping(params = {"method=uploadFile"})
public ModelAndView uploadFile(@RequestParam("myfile") MultipartFile myfile,
HttpServletRequest request, HttpServletResponse response) throws Exception {
if(!myfile.isEmpty()){
logger.info(myfile.getName());
byte[] bs= myfile.getBytes() ;
logger.info(new String(bs));
}
return null;
}
部署web应用运行后,能够看到控制台中打印出上传文件的内容,BINGO搞定(SpringMVC依赖common-fileupload.jar,需要加载该jar包)
展开全部
spring mvc 怎么在后台接收到页面上传来的值
index.jsp
<%@ page language="java" contentType="text/html; charset=Utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>欢迎</h1>
<br />
<a href="helloWorld.do">hello</a>
<br/>
<a href="login.do">login</a>
<form action="login.do" method="get">
<input type="text" value="ddddd" name="daa"/>
<input type="submit" value="试试"/>
</form>
</body>
</html>
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<display-name>springMVC</display-name>
<!--配置Sring MVC的核心控制器DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
</bean>
<!--配置控制器的映射 -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="helloWorld.do" >helloWorld</prop>
<prop key="login.do">login</prop>
</props>
</property>
</bean>
<bean id="login" class="com.examp.HelloWorldAction">
<property name="helloWorld">
<value>Hello welcome Login!</value>
</property>
<property name="viewPage">
<value>sayHello.jsp</value>
</property>
</bean>
<!--指定控制器的实现类,并且配置其参数的值 -->
<bean id="helloWorld" class="com.examp.HelloWorldAction">
<property name="helloWorld">
<value>Hello Spring World!</value>
</property>
<property name="viewPage">
<value>sayHello.jsp</value>
</property>
</bean>
</beans>
HelloWorldAction.java
package com.examp;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
index.jsp
<%@ page language="java" contentType="text/html; charset=Utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>欢迎</h1>
<br />
<a href="helloWorld.do">hello</a>
<br/>
<a href="login.do">login</a>
<form action="login.do" method="get">
<input type="text" value="ddddd" name="daa"/>
<input type="submit" value="试试"/>
</form>
</body>
</html>
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<display-name>springMVC</display-name>
<!--配置Sring MVC的核心控制器DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
</bean>
<!--配置控制器的映射 -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="helloWorld.do" >helloWorld</prop>
<prop key="login.do">login</prop>
</props>
</property>
</bean>
<bean id="login" class="com.examp.HelloWorldAction">
<property name="helloWorld">
<value>Hello welcome Login!</value>
</property>
<property name="viewPage">
<value>sayHello.jsp</value>
</property>
</bean>
<!--指定控制器的实现类,并且配置其参数的值 -->
<bean id="helloWorld" class="com.examp.HelloWorldAction">
<property name="helloWorld">
<value>Hello Spring World!</value>
</property>
<property name="viewPage">
<value>sayHello.jsp</value>
</property>
</bean>
</beans>
HelloWorldAction.java
package com.examp;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询