(1)导入jar包
(2)搭建框架
(3)在web.xml中配置dispatcherServlet的配置
<!--配置applicationcontext.xml的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置dispatcherServlet-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
<!--配置springmvc的路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置过滤器-->
<filter>
<filter-name>encode</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>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(4)视图层:
<!--
问题:
form表单默认提交数据为兼键值对数据,图片虽然会被选择,但是不会被提交,只提交了图片的名字。
解决:
文件上传前台设置:
1、设置请求方式为post
2、设置form表单属性 enctype的值为"multipart/form-data"
-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>上传和下载</title>
<%
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getcontextpath() + "/";
%>
<base href="<%=basepath%>">
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
用户:<input type="text" name="uname" value="" id="uname"><br>
爱好:<input type="text" name="hobby" value="" id="hobby"><br>
头像:<input type="file" value="上传照片" id="photo" name="photo"><br>
<input type="submit" value="提交信息">
</form>
</body>
</html>
(5)数据操作层(Mapper)
(5.1)UploadMapper.class
public interface UploadMapper {
int uploadInfo(String uname,String hobby,String photoRealName,String photoName);
}
(5.2)UploadMapper.xml
<mapper namespace="com.bjsxt.mapper.UploadMapper">
<insert id="uploadInfo">
insert into photo VALUES (default,#{0},#{1},#{2},#{3},Now());
</insert>
</mapper>
(6)业务层:(Service层)
@Service
public class UploadServieImpl implements UploadService {
@Resource
private UploadMapper uploadMapper;
@Override
public int uploadInfo(String uname, String hobby, String photoRealName, String photoName) {
return uploadMapper.uploadInfo(uname,hobby,photoRealName,photoName);
}
}
(6)Controller
@Controller
public class MyController {
@Resource
private UploadService uploadServiceImpl;
@RequestMapping("upload")
public String upload(String uname, String hobby, multipartfile photo, HttpServletRequest request) throws IOException {
//1.将文件上传到服务器的硬盘中
//创建新的文件名
//获取文件原始名
String photoRealName = photo.getoriginalFilename();
//获取文件后缀
String suffix = photoRealName.substring(photoRealName.lastIndexOf("."));
//校验文件类型
if(!(".jpg".equals(suffix)||".bmp".equals(suffix)||".png".equals(suffix))){
request.setAttribute("str","文件类型不正确,文件类型支持:jpg|bmp|png");
return "/error.jsp";
}
//生成新的文件名:UUID生成的随机数+系统当前时间的毫秒数+后缀
String photoName = UUID.randomUUID()+""+System.currentTimeMillis()+suffix;
//获取项目根目录下的资源存储目录的绝对路径
//获取ServletContext对象
ServletContext sc = request.getServletContext();
//获取资源路径
String path = sc.getRealPath("/images");
//存储上传资源
photo.transferTo(new File(path+"/"+photoName));
//上传到服务器
int i = uploadServiceImpl.uploadInfo(uname, hobby, photoRealName, photoName);
if(i==0){
return "/error.jsp";
}else {
return "/success.jsp";
}
}
}
(7)applicationcontext.xml的配置
<!--配置注解扫描-->
<context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>
<!--配置数据源bean-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.MysqL.jdbc.Driver"></property>
<property name="url" value="jdbc:MysqL://localhost:3306/text"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--配置工厂-->
<bean id="factory" class="org.mybatis.spring.sqlSessionfactorybean"></bean>
<!--配置mapper-->
<bean id="mapepr" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="factory"></property>
<property name="basePackage" value="com.bjsxt.mapper"></property>
</bean>
<!--配置事务管理:已使用注解-->
(8)springmvc.xml的配置
<!--配置注解扫描-->
<context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<!--配置注解解析器-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置静态资源放行-->
<mvc:resources mapping="/images/*" location="/images/"></mvc:resources>
<mvc:resources mapping="/js/*" location="/js/"></mvc:resources>
<!--配置上传解析bean-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置上传组件的编码格式-->
<property name="defaultEncoding" value="utf-8"></property>
<!--配置储存文件的最大容量值-->
<property name="maxInMemorySize" value="150000"></property>
<!--配置上传文件最大的尺寸-->
<property name="maxUploadSize" value="1024123123"></property>
</bean>
<!--当上传文件的大小超过限制时,会报出异常,配置异常处理的bean-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">limitSize.jsp</prop>
</props>
</property>
</bean>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。