码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC中简单的上传

时间:2014-09-21 00:31:29      阅读:480      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   java   ar   for   文件   

项目截图

bubuko.com,布布扣

需要导入的基本包有Spring所有的包加上Apache支持上传的包

bubuko.com,布布扣

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>


	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>WEB-INF/classes/spring-servlet.xml</param-value>
		</init-param>

		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 配置字符集 -->
	<filter>
		<filter-name>encodingFilter</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-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

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

	<!--自动扫描com.iss包下的所有组件 -->
	<context:component-scan base-package="com.iss"></context:component-scan>
	<mvc:annotation-driven />
	<!--配置静态资源 -->
	<mvc:resources location="/js/" mapping="/js/**" />
	<mvc:resources location="/upload/" mapping="/upload/**" />
	<mvc:resources location="/images/" mapping="/images/**" />

	<!-- SpringMVC 上传文件时,需配置multipartResolver处理器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 指定所上传文件的总大小不能超过800KB......注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
		<property name="maxUploadSize" value="800000" />
	</bean>

	<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
	<!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
	<bean id="exceptionResolver"
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->
				<prop
					key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
			</props>
		</property>
	</bean>


	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>

	</bean>


</beans>

com.iss.controller..DemoController


package com.iss.controller,DemoController.java


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/demo")
public class DemoController {


@RequestMapping("/upload")
public String goUpload() {
return "myupload";
}


}


com.iss.controller,UserController.java

package com.iss.controller;


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


import com.iss.pojo.User;


@Controller
@RequestMapping("/user")
public class UserController {


public UserController() {
// TODO Auto-generated constructor stub
}


@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUser(User user, MultipartFile myfile,
HttpServletRequest request) throws IOException {
String realPath = request.getSession().getServletContext()
.getRealPath("/upload");
System.out.println(realPath);
FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(
realPath, myfile.getOriginalFilename()));
return "myupload";


}

}


myupload,jsp


<form action="user/add" method="post" enctype="multipart/form-data">
username: <input type="text" name="username" /><br /> yourfile: <input
type="file" name="myfile" /><br />
<!-- yourfile: <input type="file" name="myfiles" /><br />
yourfile: <input type="file" name="myfiles" /><br />  -->
<input type="submit" value="添加" />
</form>

输入路径http://localhost:8080/SpringMVC_04/demo/upload 选择浏览上传然后到

tomcat/webapps/找到项目中的upload文件夹就可以看见里面上传的内容。




SpringMVC中简单的上传

标签:style   blog   http   io   os   java   ar   for   文件   

原文地址:http://blog.csdn.net/wangdianyong/article/details/39436067

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!