前言
本篇文章主要介绍的是SpringBoot实现文件上传下载。
GitHub源码链接位于文章底部。
创建maven项目,在pom文件中添加依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <!--父级依赖,它用来提供相关的 Maven 默认依赖。使用它之后,常用的springboot包依赖可以省去version 标签--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath ></relativePath> </parent> <dependencies> <!-- Spring Boot Web 依赖 核心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency> </dependencies>
application.yml文件中添加配置
server: port: 8080 spring: servlet: multipart: enabled: true # 最大支持文件大小 max-file-size: 100MB # 最大支持请求大小 max-request-size: 100MB #文件存储路径 filepath: D:/file/
文件存储路径可以修改,文件上传的大小限制这里设置的100M
controller层
在controller文件夹下创建FileController在controller文件夹下创建FileController
因为这里只进行文件上传下载,不进行业务逻辑处理,因此只用在控制层实现即可,定义文件上传、下载的接口。
@RestController @RequestMapping("/file") @Slf4j public class FileController { @Value("${filepath}") private String filepath; /** * 处理文件上传 */ @PostMapping(value = "/upload") public String uploading(@RequestParam("file") MultipartFile file) { File targetFile = new File(filepath); if (!targetFile.exists()) { targetFile.mkdirs(); } try (FileOutputStream out = new FileOutputStream(filepath + file.getOriginalFilename());){ out.write(file.getBytes()); } catch (Exception e) { e.printStackTrace(); log.error("文件上传失败!"); return "uploading failure"; } log.info("文件上传成功!"); return "uploading success"; } @RequestMapping("/download") public void downLoad(HttpServletResponse response) throws UnsupportedEncodingException { String filename="JAVA核心知识点整理.pdf"; String filePath = "D:/file" ; File file = new File(filePath + "/" + filename); if(file.exists()){ response.setContentType("application/octet-stream"); response.setHeader("content-type", "application/octet-stream"); response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename,"utf8")); byte[] buffer = new byte[1024]; //输出流 OutputStream os = null; try(FileInputStream fis= new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis);) { os = response.getOutputStream(); int i = bis.read(buffer); while(i != -1){ os.write(buffer); i = bis.read(buffer); } } catch (Exception e) { e.printStackTrace(); } } } }
html页面进行测试
在resource下创建static文件夹,在static文件夹下创建file.html
springboot项目中,静态资源默认访问目录就是resource下的static文件夹
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <form enctype="multipart/form-data" method="post" action="file/upload"> <input type="file" name="file"/> <input type="submit" value="上传"/> </form> <a href="/file/download">文件下载</a> </body> </html>
启动类:
在controller同级的目录下创建启动类FileApplication
@SpringBootApplication public class FileApplication { public static void main(String[] args) { SpringApplication.run(FileApplication.class, args); } }
测试:运行启动类,访问localhost:8080/file.html ,进行相关操作。
本文GitHub源码:https://github.com/lixianguo5097/springboot/tree/master/springboot-file
CSDN:https://blog.csdn.net/qq_27682773
简书:https://www.jianshu.com/u/e99381e6886e
博客园:https://www.cnblogs.com/lixianguo
个人博客:https://www.lxgblog.com
来源:https://www.cnblogs.com/lixianguo/p/12518970.html