2022首次更文挑战第24天 | 云盘系统的开发04

131 阅读2分钟

「这是我参与2022首次更文挑战的第24天,活动详情查看:2022首次更文挑战

这一篇,继续完善云盘系统的文件功能,因为数据是核心,所以,文件功能很重要。

云盘上传文件功能

1、file:上传的文件,userName:用户,path:上传指定的路径
2、fileRootPath + userName + "/" + path;拼接出来的是服务器上传的文件所在路径
3、if (!filePathDir.exists())判断文件夹是否存在,如果不存在则建立文件夹
4、file.getOriginalFilename();获取上传文件的原名 例464e7a80_710229096@qq.com.zip
5、FileUtils.copyInputStreamToFile(file.getInputStream(), new File(saveFilePath, saveFileName));// 上传文件到磁盘

public boolean upload(MultipartFile file, String userName, String path) {
    boolean b = false;
    String saveFilePath = fileRootPath + userName + "/" + path;
    logger.warn("1 saveFilePath:" + saveFilePath);
    File filePathDir = new File(saveFilePath);
    if (!filePathDir.exists()) {
        filePathDir.mkdir();
    }
    String saveFileName = file.getOriginalFilename();
    try {
        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(saveFilePath, saveFileName));

        b = true;
    } catch (Exception e) {
        logger.error("Exception:", e);
        return false;
    }
    return b;
}

实现效果 在这里插入图片描述

下载文件

1、fileRootPath + userName + "/" + path;服务器下载的文件所在的本地路径的文件夹
2、if (!filePathDir.exists())判断文件夹是否存在-建立文件夹
3、saveFilePath.replace(fileRootPath, "/data/");下载文件到本地路径

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 = stringSlashToOne(link);
    logger.warn("返回的路径:" + link);
    return link;
}

重命名文件

1、oldNameWithPath旧文件名称,newNameWithPath新文件名称
2、stringSlashToOne(fileRootPath + userName + "/" + path);替换多个/变成单个/
3、renameFile(oldNameWithPath, newNameWithPath)oldNameWithPath替换为newNameWithPath

public boolean userFileRename(String oldName, String newName, String userName, String path) {
    // 重命名-本地磁盘文件
    String oldNameWithPath;
    String newNameWithPath;
    if ("@dir@".equals(oldName)){
        oldNameWithPath = stringSlashToOne(fileRootPath + userName + "/" + path);
        newNameWithPath = oldNameWithPath.substring(0, (int)getfilesuffix(oldNameWithPath, true, "/")) + "/" + newName;
        newNameWithPath = stringSlashToOne(newNameWithPath);
    }
    else {
        oldNameWithPath = stringSlashToOne(fileRootPath + userName + "/" + path + "/"  + oldName);
        newNameWithPath = stringSlashToOne(fileRootPath + userName + "/" + path + "/"  + newName);
    }
    return renameFile(oldNameWithPath, newNameWithPath);
}

实现效果 在这里插入图片描述

转码问题

文件的所有格式统一转成UTF-8,这里用到了之前讲的des.encode加密方法,因为现在文件也是很重要的,需要进行加密操作。

public String fileShareCodeEncode(String filePathAndName) {
    EncryptUtil des;
    try {
        des = new EncryptUtil(key, "utf-8");
        return des.encode(filePathAndName);
    } catch (Exception e) {
        logger.error("Exception:", e);
    }
    return "null";
}