1,背景前提
本人系一名正在学习Spring框架的小白,因为某次自己负责的部分需要用到文件上传下载的功能,然后再网上找到的大多是只能在windows上运行的,有的用上了也是各种问题,刚好自己看的教程里面,学习到了这一个部分,于是自己便抱着试一试的态度,在网上搜索了相应的依赖(教程里用到的是jar包)
2,使用的依赖
这里直接在下面给出依赖包
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>2.4</version>
</dependency>
3,文件的上传下载Controller类
package com.try1.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;
/**
* 文件上传,下载类
*/
@Controller
public class FileUploadController {
@RequestMapping("download")
public void download(String fileName, HttpServletResponse res, HttpServletRequest req) throws IOException{
//设置响应流中文件进行下载
res.setHeader("Content-Disposition", "attachment;filename="+fileName);
// res.setHeader("Content-Disposition", "attachment;filename=bbb.txt");
//把二进制流放入到响应体中.
ServletOutputStream os = res.getOutputStream();
//windows下目录
// String path = FileUploadController.class.getResource("/files/upload").getPath().replace('\\', '/');
String path="/files/images";
System.out.println(path);
File file = new File(path, fileName);
byte[] bytes = FileUtils.readFileToByteArray(file);
os.write(bytes);
os.flush();
os.close();
}
@RequestMapping("upload")
public String upload(MultipartFile file,String name) throws IOException {
System.out.println("name"+name);
String filename=file.getOriginalFilename();
String suffix=filename.substring(filename.lastIndexOf("."));
String uuid= UUID.randomUUID().toString();
//windows下目录
// String path = FileUploadController.class.getResource("/files/upload/").getPath().replace('\\', '/');
String path="/files/images";
System.out.println(path);
FileUtils.copyInputStreamToFile(file.getInputStream(),new File(path+name+suffix));
return "redirect:/index.html";
}
}
4,值得注意的细节
这里在用目录的地方遇到了一个问题,jar包打包运行到Linux上的时候,目录结构是有很大区别的。所以我上方的代码里面注释了一行,那个是我原来windows的搞法,如果不做改变,会生成一个10层目录。wdnmd。
当时我也很懵,刚开始是准备复制好了这个文件夹目录,然后自己在前端加上相应的前缀来给予显示的。但是。。事情完全不如我所料。
所以我改变了策略,这里是在用了tomcat的文件服务器技术,在Linux的~下生成了一个/files的文件夹,这样前端就能显示出来了。
这里同样给出前端测试代码
<!DOCTYPE html>
<html lang="en" class="no-js">
<!--<![endif]-->
<head>
<meta charset="UTF-8" />
<!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> -->
<title>Login and Registration Form with HTML5 and CSS3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
<meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
<meta name="author" content="Codrops" />
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" type="text/css" href="/css/login/demo.css" />
<link rel="stylesheet" type="text/css" href="/css/login/style.css" />
<link rel="stylesheet" type="text/css" href="/css/login/animate-custom.css" />
</head>
<body>
<a href="download?fileName=41e00674-b0ae-4b7c-9dec-c4de763e5bc0.txt">下载</a>
<form action="upload" enctype="multipart/form-data"method="post">
姓名:<input type="text" name="name"/></br>
文件:<input type="file" name="file"/></br>
<input type="submit" value="提交"/>
</form>
</body>
</html>