Python+HDFS实现云盘系统

一、项目任务描述

云盘系统通过互联网为企业和个人提供信息的存储、读取、下载等服务。具有安全稳定、海量等特点。目前,云盘系统逐步走向成熟,特别是公有云盘能够向用户提供免费存储,,离线下载,文件智能分类等功能。随着互联网的不断普及,用户存储在云盘上的数据量越来越大,数据类型页越来越多样化,不仅有传统的文本文件,二进制文件,还有视频,音频,图像,图片等。这些数据不仅类型繁多,而且数据量普遍庞大。例如,一个视频文件可能高达几个G甚至几十个G。因此充分利用大数据平台的优势,可以为云盘提供一套适合上述特点的底层环境。
要求开发一个基于HDFS的云盘系统来实现云盘的基本数据存储和访问功能。
功能需求:
1.用户管理
用户的注册,登录,退出。注册成功时需要将用户信息写入数据库,并在HDFS中建立用户的专属目录。
2.文件管理
提供文件上传,下载,浏览,删除等功能。提供目录的管理,可以创建目录,可以删除目录。删除目录时需要给出提示。
3.界面设计
为系统设计并实现主界面,基于窗口或基于浏览器均可。所有操作均在人机交互界面中完成。

二、分析与设计

1、系统架构及功能模块的划分

在这里插入图片描述

2、模块设计思想

①下载:
从HDFS中获取到文件名,然后打开本地窗口命名保存,调用download函数
②删除文件:
判断是否需要删除,如果需要删除就将窗口中选中的文件路径获取到,然后调用delete函数
③创建文件:
判断是否是文件夹,如果是文件夹就执行,获取选中的文件夹的位置,然后调用makedirs函数,将文件夹创建到选中的文件夹里,如果是点击的文件创建,就将文件夹创建到根目录下。
④上传文件:
打开本地窗口选中文件,如果选择了文件就返回一个路径,将想要保存的路径取出然后调用upload函数

3. 人机界面设计(如果有)

新建文件夹窗口:
利用tkinter编写一个输入文件夹名字的窗口,点击确定返回输入的值,将其传进makedirs函数

4. 相关类的说明

(1)类中数据成员和函数成员的说明;
①服务器连接对象client
②root,window窗口对象
③address地址对象
④dir_name输入变量
⑤tk是tkinter的缩写
⑥新建文件夹函数new_dir(path,username)
⑦上传文件函数put_to_hdfs(path,username)
⑧下载文件函数get_from_hdfs(path)
删除文件函数delete_file(path,username)

(2)各个类中体现关键操作的成员函数的实现思路,实现思路可采用流程图进行说明。
在这里插入图片描述

三、系统实现

1、各个类的定义与实现

①打开本地窗口

#上传打开本地窗口
def load_window():
    root = tk.Tk()
    root.withdraw()
    path=filedialog.askopenfilename()
    return path
#下载并命名显示本地窗口
def savefile_window():
    root = tk.Tk()
    root.withdraw()
    name=filedialog.asksaveasfilename()
    return name

②下载文件函数

# 下载文件
def get_from_hdfs(path):
    address=savefile_window()#调用保存文件窗口
    client.download(path,address)

③删除文件函数

# 删除文件
    def delete_file(path,username):
        if path!=rootPath1:  #操作非根目录
            try:
                str1=tk.messagebox.askyesno('提示','是否需要删除?')
                if str1:
                    client.delete(path)
                    window.destroy()
                    root = tkinter.Tk()
                    root.withdraw()
                    tk.messagebox.showinfo(title='提示', message='操作成功!')
                    main_face(username)
                
            except:
                tk.messagebox.showinfo(title='提示', message='文件夹不为空!')
        else:#提示根目录不能删除
            root = tkinter.Tk()
            root.withdraw()
            tk.messagebox.showinfo(title='提示', message='此目录不能删除!')
            main_face(username)

④创建文件夹函数

 # 创建文件
    def new_dir(path,username):
        if(client.status(path)['type'] == 'DIRECTORY'):#如果是文件夹就执行
            root=tk.Tk()
            root.title('创建文件夹')
            dir_name=tk.StringVar(root,value='')
            tk.Label(root,text='请输入名称:').place(x=10,y=10)
            tk.Entry(root,textvariable=dir_name).place(x=10,y=30)
            def get_dir_name():
                dir_names = dir_name.get()
                if dir_names=='':
                    root.destroy()
                    window.destroy()
                    root1 = tkinter.Tk()
                    root1.withdraw()
                    tk.messagebox.showinfo(title='提示', message='名称为空!')
                    main_face(username)
                else:
                    client.makedirs(path+'/'+dir_name.get(),permission=777)
                    root.destroy()
                    window.destroy()
                    root1 = tkinter.Tk()
                    root1.withdraw()
                    tk.messagebox.showinfo(title='提示', message='操作成功!')
                    main_face(username)
            tk.Button(root,text='确定',command=get_dir_name).place(x=150,y=30)
        else:#否则不执行
            window.destroy()
            root2 = tkinter.Tk()
            root2.withdraw()
            tk.messagebox.showinfo(title='提示', message='操作失败!')
            main_face(username)

⑤上传文件函数

# 上传文件
    def put_to_hdfs(path,username):
        address=load_window()
        if address=='':#点击取消之后执行
            window.destroy()
            root = tkinter.Tk()
            root.withdraw()
            tk.messagebox.showinfo(title='提示', message='请选择文件!')
            main_face(username)
        else:#点击文件之后执行
            filename=os.path.basename(address)
            if  filename in client.list(path[-2]):              
                window.destroy()
                root = tkinter.Tk()
                root.withdraw()
                tk.messagebox.showinfo(title='提示', message='文件已存在!')
                main_face(username)
            else:
                if(client.status(path[-1])['type'] == 'DIRECTORY'): #上传到文件夹中

                    client.upload(path[-1],address)
                    window.destroy()
                    root = tkinter.Tk()
                    root.withdraw()
                    tk.messagebox.showinfo(title='提示', message='操作成功!')
                    main_face(username)
                else:#上传到根目录

                    client.upload(path[-2],address)
                    window.destroy()
                    root = tkinter.Tk()
                    root.withdraw()
                    tk.messagebox.showinfo(title='提示', message='操作成功!')
                    main_face(username)

2、实现测试程序

①上传
在这里插入图片描述
在这里插入图片描述
②下载
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
③删除
在这里插入图片描述
在这里插入图片描述
④新建文件夹
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
⑤新建到当前目录
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、用户使用说明

命名时注意使用正确的命名方式,路径需要注意使用正确的路径,操作过程中需要按照步骤一步一步来,不要进行跨越式操作,系统运行缓慢时,请耐心等待。

五、参考文章

https://blog.csdn.net/u014578907/article/details/88258574
https://blog.csdn.net/Time_book/article/details/105903696
https://www.cnblogs.com/it-tsz/p/10582501.html
https://www.cnblogs.com/it-tsz/p/10582493.html
https://www.cnblogs.com/kaishirenshi/p/8611358.html
https://blog.csdn.net/weixin_40283816/article/details/83387965

上述部分是我完成的,剩下的是我同伴完成的,这里就不方便展示了,若有需要完整项目的代码请移步到这里

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
实现Spring Boot与HDFS和MySQL的文件上传和下载,需要先配置Hadoop和MySQL环境。然后,需要添加相应的依赖项并编写以下代码: 1. 配置HDFS 在application.properties文件中添加以下配置: ``` # HDFS配置 hadoop.hdfs.path=hdfs://localhost:9000 hadoop.hdfs.username=hadoop ``` 2. 配置MySQL 在application.properties文件中添加以下配置: ``` # MySQL配置 spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 添加依赖项 在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>com.mysql.cj</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 4. 编写上传和下载代码 上传代码: ```java @Service public class HdfsService { @Value("${hadoop.hdfs.path}") private String hdfsPath; @Value("${hadoop.hdfs.username}") private String hdfsUsername; @Value("${spring.servlet.multipart.location}") private String uploadPath; @Autowired private FileSystem fileSystem; @Autowired private JdbcTemplate jdbcTemplate; public void upload(MultipartFile file) throws IOException { String fileName = file.getOriginalFilename(); String filePath = "/upload/" + fileName; Path path = new Path(hdfsPath + filePath); FSDataOutputStream outputStream = fileSystem.create(path); outputStream.write(file.getBytes()); outputStream.close(); jdbcTemplate.update("INSERT INTO file (name, path) VALUES (?, ?)", fileName, filePath); } } ``` 下载代码: ```java @Service public class HdfsService { @Value("${hadoop.hdfs.path}") private String hdfsPath; @Value("${hadoop.hdfs.username}") private String hdfsUsername; @Value("${spring.servlet.multipart.location}") private String uploadPath; @Autowired private FileSystem fileSystem; @Autowired private JdbcTemplate jdbcTemplate; public void download(HttpServletResponse response, String fileName) throws IOException { String filePath = jdbcTemplate.queryForObject("SELECT path FROM file WHERE name = ?", String.class, fileName); Path path = new Path(hdfsPath + filePath); FSDataInputStream inputStream = fileSystem.open(path); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); } } ``` 以上代码将文件存储在HDFS中,并将文件名和路径保存到MySQL中。下载时,从MySQL中查询文件路径并将文件流发送到响应中。注意,在这里我们使用了Apache Commons IO库的IOUtils类来将文件流复制到响应中。 同时,我们还需要在控制器中编写上传和下载的端点: ```java @RestController public class FileController { @Autowired private HdfsService hdfsService; @PostMapping("/upload") public void upload(@RequestParam("file") MultipartFile file) throws IOException { hdfsService.upload(file); } @GetMapping("/download") public void download(HttpServletResponse response, @RequestParam("fileName") String fileName) throws IOException { hdfsService.download(response, fileName); } } ``` 现在,我们已经完成了Spring Boot与HDFS和MySQL的文件上传和下载。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值