文件上传是文件由浏览器复制到服务器,文件下载是文件从服务器复制到浏览器 。
文件上传要求form表单的请求方式必须为post,并且添加属性enctype="multipart/form-data",SpringMVC中将上传的文件封装到MultipartFile对象中,通过此对象可以获取文件相关信息。
使用ResponseEntity实现下载文件的功能,ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的完整的响应报文。
1.上传步骤:
a. pom文件中添加依赖
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
b. springmvc.xml文件中配置bean
<!--必须通过文件解析器的解析才能将文件转换为MultipartFile对象-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 字节计算 -->
<!--<property name="maxUploadSize" value="104857600"/>-->
<property name="defaultEncoding" value="UTF-8" />
</bean>
c. html文件中form表单提交
<form action="/file/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="文件下载">
</form>
d. 在controller层提供一个控制器方法
@RequestMapping("/upload")
public String upload(MultipartFile file) throws IOException {
//获取上传的文件的文件名
String fileName = file.getOriginalFilename();
String[] split = fileName.split("\\.");
int length = split.length;
// 获取后缀名
String suffixName = split[length - 1];
// long l = System.currentTimeMillis();
// 格式化日期充当文件名
String name = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
// 上传文件的路径
String path = "C:\\Users\\83582\\Desktop";
// 文件重命名
String newName = path + File.separator + name + "." + suffixName;
File f = new File(newName);
//实现上传功能
file.transferTo(f);
return "success";
}
如此,上传功能即可实现。
2.下载步骤:
a. html文件中使用超链接,url路径中的 ? 后边 name 的值为你需要下载的图片名称
<a href="/file/down?name=preview.jpg">文件下载</a>
b. 提供控制器方法
@RequestMapping("/down")
public ResponseEntity<byte[]> downFile(String name) throws IOException {
// 需要下载的图片的路径地址
String path = "C:\\Users\\83582\\Pictures\\Saved Pictures";
File file = new File(path + File.separator + name);
// http 请求头
HttpHeaders headers = new HttpHeaders();
// 设置请求头信息
headers.setContentDispositionFormData("attachment",name);
// 设置信息类型
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(
// 把文件转成字节流进行响应
FileUtils.readFileToByteArray(file),
headers,
HttpStatus.OK
);
}
如此,下载功能即可实现。
3.上传和下载的controller文件:
@Controller
@RequestMapping("/file")
public class FileUpAndDownController {
@RequestMapping("/upload")
public String upload(MultipartFile file) throws IOException {
//获取上传的文件的文件名
String fileName = file.getOriginalFilename();
String[] split = fileName.split("\\.");
int length = split.length;
// 获取后缀名
String suffixName = split[length - 1];
String name = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
String path = "C:\\Users\\83582\\Desktop";
String newName = path + File.separator + name + "." + suffixName;
File f = new File(newName);
//实现上传功能
file.transferTo(f);
return "success";
}
@RequestMapping("/down")
public ResponseEntity<byte[]> downFile(String name) throws IOException {
String path = "C:\\Users\\83582\\Pictures\\Saved Pictures";
File file = new File(path + File.separator + name);
// http 请求头
HttpHeaders headers = new HttpHeaders();
// 设置请求头信息
headers.setContentDispositionFormData("attachment",name);
// 设置信息类型
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(
// 把文件转成字节流进行响应
FileUtils.readFileToByteArray(file),
headers,
HttpStatus.OK
);
}
}
在此,祝各位帅哥美女,步步高升,一帆风顺!!!