自己动手用Springboot实现仿百度网盘

这个博客展示了如何使用Springboot构建一个类似于百度网盘的系统,包括文件的上传、下载、删除、重命名、目录管理和分享等功能。通过文件操作、文件信息列表展示、文件类型判断以及文件大小转换等技术实现。
摘要由CSDN通过智能技术生成

return b;

}

@Override

public String download(String fileName, String userName, String path) {

// 服务器下载的文件所在的本地路径的文件夹

String saveFilePath = fileRootPath + userName + “/” + path;

logger.warn(“1 saveFilePath:” + saveFilePath);

// 判断文件夹是否存在-建立文件夹

File filePathDir = new File(saveFilePath);

if (!filePathDir.exists()) {

filePathDir.mkdir();

}

// 本地路径

saveFilePath = saveFilePath + “/” + fileName;

String link = saveFilePath.replace(fileRootPath, “/data/”);

link = StringUtil.stringSlashToOne(link);

logger.warn(“返回的路径:” + link);

return link;

}

@Override

public List userFileList(String userName, String path) {

logger.warn(“执行userFileList函数!”);

List fileMsgList = new ArrayList<>();

// 拉取文件列表-本地磁盘

String webSaveFilePath = fileRootPath + userName + “/” + path;

File files = new File(webSaveFilePath);

if (!files.exists()) {

return fileMsgList;

}

File[] tempList = files.listFiles();

if (tempList == null) {

return fileMsgList;

}

for (File file : tempList) {

if (file.isFile()) {

FileMsg fileMsg = new FileMsg();

// 获取文件名和下载地址

String link = file.toString().replace(“\”, “/”);

String[] nameArr = link.split(“/”);

String name = nameArr[nameArr.length - 1];

link = link.replace(fileRootPath, “/data/”);

link = link.replace(“/root/pan/”, “/data/”);

String size = FileUtil.fileSizeToString(file.length());

SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

String lastModTime = formatter.format(file.lastModified());

// 赋值到json

fileMsg.setName(name);

fileMsg.setLink(link);

fileMsg.setSize(size);

fileMsg.setTime(lastModTime);

if (FileUtil.isMp4(name)) {

fileMsg.setType(“mp4”);

} else if (FileUtil.isVideo(name)) {

fileMsg.setType(“video”);

} else {

fileMsg.setType(“file”);

}

fileMsgList.add(fileMsg);

} else {

FileMsg fileMsg = new FileMsg();

String link = file.toString().replace(“\”, “/”);

String[] nameArr = link.split(“/”);

String name = nameArr[nameArr.length - 1];

String dirPath = link.replace(fileRootPath + userName, “”);

if (!name.equals(“userIcon”)) {

fileMsg.setName(name);

fileMsg.setSize(“Directory”);

fileMsg.setType(“dir”);

fileMsg.setLink(dirPath);

SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

String lastModTime = formatter.format(file.lastModified());

fileMsg.setTime(lastModTime);

fileMsgList.add(fileMsg);

}

}

}

//排序

ListUtil.listSort(fileMsgList);

return fil

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值