springmvc文件上传和中文处理

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">
  <display-name>springmvcday02_annotation</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>
  
  <!-- 配置springmvc核心控制器 -->
  <servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 设置初始化优先级,数字越小越高,会在部署到tomcat时就被初始化创建 -->
		<load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 配置springmvc入口 -->
  <servlet-mapping>
  		<servlet-name>springmvc</servlet-name>
  		<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- 配置过滤器
  	        乱码问题处理:1.在web.xml文件中配置过滤器(post方式解决)
  	        	2.在tomcat的server.xml文件中的contoter节点下进行配置(get方式提交解决)
   -->
  <filter>
  		<filter-name>EncodingFilter</filter-name>
  		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  		<init-param>
  			<!-- 配置编码格式为utf-8 -->
  			<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-mapping>
  		<filter-name>EncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

springmvc-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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/context http://www.springframework.org/schema/context/spring-context.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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


	<!-- 加载推荐使用的驱动 -->
	<mvc:annotation-driven/>
	<!-- 开启注解扫描包 -->
	<context:component-scan base-package="com.action"></context:component-scan>
	
	<!-- HandlerMapping和HandlerAdaptaher在服务被部署时就已经被创建 -->
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置视图解析路径 -->
		<!-- prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp"  -->
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置文件上传功能 -->
	<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
		<!-- 配置编码格式 -->
		<property name="DefaultEncoding" value="utf-8"></property>
		<!-- 配置文件上传大小:5M(自定义) 1024*1024*5 -->
		<property name="maxUploadSize" value="5242880"></property>
	</bean>
	
	<!-- 配置拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 配置拦截器要拦截的路径 ,/**拦截所有目录-->
			<mvc:mapping path="/**"/>
			<!-- 配置自定义拦截器 -->
			<bean class="com.action.MyHandlerInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
	


	
</beans>

view:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%  
    String path = request.getContextPath();  
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>      
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 
		文件上传要设置上传方式为post
		对表单的数据不进行编码设置(enctype="multipart/form-data")
		配置文件上传入口 -->
	<form action="mycontroller/hello13.do" method="post" enctype="multipart/form-data">
		 <input type="file" name="file"><br/>
		 <input type="file" name="file"><br/>
		 <input type="file" name="file"><br/>
		 <input type="submit" value="开始上传">
	</form>
</body>
</html>

controller:

	/**
	 * 文件上传(单文件/多文件)
	 * @return 
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value="/hello13",method=RequestMethod.POST)
	public ModelAndView hello13(@RequestParam("file")MultipartFile[] files) throws IllegalStateException, IOException{
		//对传过来的文件进行判断
		if(files!=null){
			for(int i=0;i<files.length;i++){
			//transferTo将文件复制到另一个文件里面
			//getOriginalFilename获得传入的文件的原始文件名
			files[i].transferTo(new File("D:\\test\\"+files[i].getOriginalFilename()));//拼接目录
			}
		}
		
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("hello");
		modelAndView.addObject("msg","文件上传成功!");
		return modelAndView;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值