目的:使用springmvc上传文件/图片
项目整体预览:
第一步:导入相对应的jar包 ()
第二步: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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>qiniu-upload-img</display-name>
<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>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认找的是 /WEB-INF/[servlet的名称]-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
1、 /* 拦截所有(jsp js png .css) 真的全拦截 建议不使用
2、 *.action *.do 拦截以do action 结尾的请求
3、 / 拦截所有(不包括jsp) 强烈建议使用 对静态资源放行
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
第三步:springmvc.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描@controler @Service 等注解 -->
<context:component-scan base-package="com.ziyang.upload.controller" />
<mvc:annotation-driven/>
<!-- 上传图片配置实现类 -->
<!-- id和class 都是固定的 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传图片的大小 -->
<property name="maxUploadSize" value="100000000000"></property>
</bean>
<!-- 视图解释器 -->
<!-- 用于配置访问地址的前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".html"/>
</bean>
</beans>
第四步:index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8080/qiniu-upload-img/bbb.action" method="post" enctype="multipart/form-data">
<input type="file" name="img" value="请选择文件"/> <br />
<input type="submit" />
</form>
</body>
</html>
第五步:测试controller
package com.ziyang.upload.controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UploadController {
@RequestMapping(value = "/bbb.action")
public String test1(MultipartFile img) throws IOException{
//方法一 获取相对应的流
InputStream stream = img.getInputStream();
//直接存储到某个文件位置
File file = new File("填写你的路径");
img.transferTo(file);
return "index";
}
}
第六步演示:
打开浏览器:http://localhost:8080/qiniu-upload-img/index.html
选择文件后点击确定。