栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于SpringBoot的文件管理系统,全教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于SpringBoot的文件管理系统,全教程

这篇文章讲解如何用SpringBoot做一个简单的文件管理系统,还是用SpringBoot来搭建项目吧,方便一点。

假如我C盘有一个upload文件夹,这个是资源管理的根目录。

我们要做的,就是在网页上显示这个目录,然后可以对这个目录里面的资源进行一些操作,比如压缩,下载。

没错,就这么简单。

把选中的三个文件删掉,这样看起来清爽一点。

去https://mvnrepository.com/上搜索Hutool

不管,我们就用最新的。


    cn.hutool
    hutool-all
    5.7.3

然后relaod一下。

 

server:
  port: 80

root:
  diskpath: C:upload

diskpath是我们用于资源管理的根目录。

创建一个IndexController

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(){
        return "index";
    }

}

意图很明显,就是为了返回一个叫做index的页面。

但是,我们现在还没有index页面。

添加依赖


    org.springframework.boot
    spring-boot-starter-thymeleaf

注意,每次加了新的依赖,一定要maven - reload!

配置页面路径:

spring:
  thymeleaf:
    prefix: classpath:/templates/

index.html放在这里




    
    资源管理


  Hello world!

重启项目,访问:http://localhost/

成功了。

 获取目录中所有的文件

IndexController.java

@Controller
@ConfigurationProperties(prefix = "root")
@Data
public class IndexController {

    private String diskpath;

    @RequestMapping("/")
    public String index(Model m){
        File[] files = FileUtil.ls(diskpath);
        m.addAttribute("files",files);
        return "index";
    }

}

加@Data是为了自动生成set方法,这样才能让@ConfigurationProperties(prefix = "root")自动去读取yml中的数据。

添加一个依赖,至于为什么要添加,这个在SpringBoot教程里面讲过了。


    org.springframework.boot
    spring-boot-configuration-processor
    2.5.1
    true

注意版本号得是2.5.1,不写版本号默认去下载2.5.2了,我的idea默认的maven下载不到这个jar,估计是源头仓库就没有。

这个问题让站长纠结了好半天。

index.html




    
    资源管理


  

效果:

丑一点没关系,我们先把功能给实现了。

  • [[${file.getName()}]] 打包下载

和之前不同,这边不用th:text了,然后在每个文件旁边加一个打包下载的超链接。

打包文件

在IndexController中添加一个新的方法

@RequestMapping("/download")
@ResponseBody
public void index(String file){
    String targetName = diskpath + File.separator + file;
    //打包到当前目录
    ZipUtil.zip(targetName);
}

ZipUtil是Hutool提供的,我们直接拿来用即可。

最后一步就是把文件下下来

重新写download方法,增加下载功能

@RequestMapping("/download")
@ResponseBody
public void download(String file, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException {
    String targetName = diskpath + File.separator + file;
    //打包到当前目录
    ZipUtil.zip(targetName);
    targetName = targetName+".zip";
    response.setContentType("application/force-download");
    //解决下载文件名中文不显示的问题
    String fileName = new String(file.getBytes("utf-8"),"ISO8859-1");
    response.addHeader("Content-Disposition", "attachment;fileName=" + fileName + ".zip");

    //输出流,下载文件
    byte[] buffer = new byte[1024];
    try {
        FileInputStream fis = new FileInputStream(targetName);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream os = response.getOutputStream();
        int i = bis.read(buffer);
        while (i != -1) {
            os.write(buffer, 0, i);
            i = bis.read(buffer);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }


}

如何下载完毕后就删掉zip文件

很简单,在下载完毕后,立刻调用删除的方法即可。注意,删文件之前,一定要关闭流!

核心代码如下:

//输出流,下载文件
byte[] buffer = new byte[1024];
try {
    FileInputStream fis = new FileInputStream(targetName);
    BufferedInputStream bis = new BufferedInputStream(fis);
    OutputStream os = response.getOutputStream();
    int i = bis.read(buffer);
    while (i != -1) {
        os.write(buffer, 0, i);
        i = bis.read(buffer);
    }
    //关闭流
    fis.close();
    os.close();
    boolean del = FileUtil.del(targetName);
    System.out.println(del);
} catch (IOException e) {
    e.printStackTrace();
}

因为本项目非常简单,如果感兴趣地话,个人建议大家自己动手练一练。(毕竟也没多少行代码)

 成果展示

比如我点击第二个打包下载。

就真的下载下来了。

而且,压缩包是自动删掉的。

 

好了,以上就是用SpringBoot搭建文件下载平台的所有内容,有什么想法欢迎在评论区留言!

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号