一、上传
1、文件上传概述
- 表单上传文件:
(1)form表单的method属性设置为post。
(2)form表单的enctype属性为multipart/form-data.
(3)提供<input type=“file” name=“filename” / > 的文件上传输入框(备注:多文件multiple=“multiple”) - 配置文件设置
设置CommonsMultipartResolver实现类
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!-- 置请求编码格式 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- 设置允许上传文件的最大值 (2MB) ,单位为字节 -->
<property name="maxUploadSize" value="2097152" />
<!-- 缓存中的最大尺寸 -->
<property name="maxInMemorySize" value="2097152" />
<!-- 推迟文件解析,以便在 Controller 中捕获文件大小异常-->
<property name="resolveLazily" value="true" />
</bean>
- 导入jar
2、示例:
1、导包
2、编写web.xml
3、编写springmvc-config.xml
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!-- 置请求编码格式 -->
<property name="defaultEncoding" value="UTF-8" />
</bean>
4、编写页面jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/fileUpload" method= "post"
enctype="multipart/form-data">
请选择文件: <input id= "file" name="uploadfile" type="file" multiple="multiple" /><br />
<input type ="submit" value= "上传" />
</form>
</body>
</html>
5、编写controller
@RequestMapping("/fileUpload")
public void fileUpload(List<MultipartFile> uploadfile){
if (!uploadfile.isEmpty() && uploadfile.size() > 0) {
for (MultipartFile file: uploadfile) {
//获取上传文件的原始名称
String oldName = file.getOriginalFilename();
String destUrl = "D://upload";
File destFile = new File(destUrl);
if (!destFile.exists()) {
destFile.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String newName = sdf.format(new Date()) + "_" + oldName;
try {
file.transferTo(new File(destUrl + File.separator + newName));
} catch (IOException e) {
e.printStackTrace();
}
System.err.println("上传成功");
}
} else {
System.err.println("没有文件");
}
}
二、下载
实现文件下载,大致可分为如下两个步骤。
(1)前端页面设置a标签
<form>
<a href="${pageContext.request.contextPath}/download?filename=1.jpg" >文件下载</a>
</form>
(2)后端编写下载方法
@RequestMapping("/download")
public ResponseEntity<byte[]> download(String filename, HttpServletRequest request) throws IOException {
//指定要下载的文件所在路径
String path = "D://upload";
//创建该文件对象
File file = new File(path + File.separator + filename);
//对文件名编码,防止中文文件乱码
filename = this.getFilename(request, filename);
//设置响应头
HttpHeaders headers = new HttpHeaders();
//通知浏览器以下载的方式打开文件EE 企业银应用开发航程
headers.setContentDispositionFormData("attachment", filename);
// 定义以流的形式下载返回文件数据
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// 使用 Sring MVC 框架的 ResponseEntity 对象封装返回下载数据
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]> (FileUtils.readFileToByteArray(file) ,
headers, HttpStatus.OK);
return responseEntity;
}
private String getFilename(HttpServletRequest request, String filename) throws IOException {
// IE 不同版本 User-Agent 中出现的关键词
String[] IEBrowserKeyWords = {
"MS1E", "Trident", "Edge"};
// 获取请求头代理信息
String userAgent = request.getHeader("User-Agent");
for (String keyWord : IEBrowserKeyWords) {
if (userAgent.contains(keyWord)) {
//IE 内核浏览器,统一为 UTF-8 编码显示
return URLEncoder.encode(filename, "UTF-8");
}
}
// 火狐等其他浏览器统一为 1SO-8859-1 编码显示
return new String(filename.getBytes("UTF-8") , "ISO-8859-1");
}