文件的上传与下载
一、文件的上传:
1.创建项目:
2.导入所需的jar包:
3.设置页面表单:
<%@ 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="tsext/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<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>
==表单提交方式必须是post方式提交,enctype必须是multipart/form-data==
4.Controller中接收数据:
package com.sxt.controller;
@Controller
public class UserController {
@RequestMapping("/upload")
@ResponseBody
public void upload(String name, MultipartFile file1) throws Exception {
System.out.println(name + " " + file1.getOriginalFilename());
file1.transferTo(new File("d:/vc/" + file1.getOriginalFilename()));
}
}
5.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.sxt.controller"/>
<!-- 开启SpringMVC注解的方式 -->
<mvc:annotation-driven/>
<!-- 配置文件上传的处理器 -->
<bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
</beans>
6.实现:
二、文件的下载:
1.方式一:基于ResponseEntity实现
@RequestMapping("/download1")
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
File file = new File("d:/vc/2.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[] b = new byte[1024];
while ((num = in.read(b)) != -1) {
out.write(b, 0, num);
}
out.flush();
out.close();
in.close();
}
2.通用下载实现:
@RequestMapping("/download2")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {
// 需要下载的文件
File file = new File("d:/vc/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;
}