创建项目:
jar包:
Usercontroller类:
@Controller
public class Usercontroller {
/**
* 文件的上传
* @param username
* @param userface
* @throws Exception
*/
@RequestMapping("upload")
@ResponseBody
public void upload(String username,MultipartFile userface) throws Exception{
System.out.println(username+"==");
userface.transferTo(new File("C:/Users/",userface.getOriginalFilename()));
}
/**
* 文件的下载
* @param req
* @param pose
* @throws Exception
*/
@RequestMapping("download")
public void download(HttpServletRequest req,HttpServletResponse pose) throws Exception{
File fiel=new File("C:/Users/2.jpg");//下载文件路径
InputStream in=new FileInputStream(fiel);
// 响应的头部信息
pose.setCharacterEncoding("utf-8");
pose.setContentType("multipart/form-data");
pose.setHeader("Content-Disposition", "attachment;fileName=" + fiel.getName());
ServletOutputStream out= pose.getOutputStream();
int num=0;
byte[] a= new byte[1024];
while ((num=in.read(a))!=-1){
out.write(a, 0, num);
}
out.flush();
out.close();
in.close();
}
/**
* 基于ResponseEntity实现下载
* @param request
* @return
* @throws IOException
*/
@RequestMapping("/download2")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {
// 需要下载的文件
File file = new File("C:/Users/2.jpg");
byte[] body = null;
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=" + file.getName());
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
return entity;
}
}
Spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.ytl.controller"/>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 设置上传文件信息参数 -->
<!-- 设置文件上传的最大尺寸 -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
name="multipartResolver">
<property name="maxUploadSize" value="5242880">
</property>
</bean>
</beans>
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>Spring-MVC</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>
<init-param>
<description>关联SpringMVC的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 防止被拦截 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
</web-app>
user.jsp
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<img src="img/2.jpg" width="200" height="320">
<h2 ><a href="download">下载文件1</a></h2>
<h2 ><a href="download2">下载文件2</a></h2>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>用户图像</td>
<td><input type="file" name="userface"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
测试下载:
这里静态资源已经配置号了所以网页中会有一张图片:
点击下载(这里是我的谷歌下载的文件夹):
下载后:
左下角显示图片已经下载了:
这里也有了2.jpg的图片
上传:
选择了上传的图片:
这里是我的上传的文件夹:
提交后文件夹中就有了多了一个照片上传完成: