文章目录
文件上传
相关jar包
页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1><a href="download">下载文件1</a><br><a href="download2">下载文件2</a></h1>
<form action="upload" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="name"><br>
图 片:<input type="file" name="file1"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
配置文件
<?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.sxt.controller"/>
<!-- 开启SpringMVC注解的方式 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置文件上传的处理器 -->
<bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880"></property>
</bean>
</beans>
Controller
Controller
public class UserContorller {
@RequestMapping("/upload")
@ResponseBody
public void uploade(String name, MultipartFile file1) throws Exception {
System.out.println(name + " " + file1.getOriginalFilename());
file1.transferTo(new File("D:/文件上传demo/" + file1.getOriginalFilename()));
}
}
测试
文件下载
第一种方式
Controller
@RequestMapping("/download")
public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
File file = new File("D:/文件上传demo/1.jpg");
InputStream in = new FileInputStream(file);
// 设置响应请求头信息
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName());
ServletOutputStream out = response.getOutputStream();
// 文件复制
int num = 0;
byte[] bs = new byte[1024];
while ((num = in.read(bs)) != -1) {
out.write(bs, 0, num);
}
out.flush();
out.close();
in.close();
}
第二种方式(基于ResponseEntity实现)
@RequestMapping("download2")
public ResponseEntity<byte[]>download() throws Exception{
//需要下载的文件
File file = new File("D:/文件上传demo/1.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;
}
结果和第一种是一样的。jsp页面还有配置文件不变
静态资源处理
在SpringMVC中,默认情况下,所有的静态资源都会被拦截(js,css。html,图片、视频、音频),对于静态资源,需要手动配置静态资源过滤。
解决这个问题的方式有两种:
在web.xml文件中配置default servlet
<!-- 防止资源文件被spring MVC拦截 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
在配置文件中通过标签设置
<!-- 防止资源文件被spring MVC拦截-->
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>