""" Author : XinYi Song Time : 2021/11/3 14:29 Desc: """ from util.file_store_path import file_store_path_day, file_store_path_year, file_store_path_month """ 实现文件断点续传 """ import sys import os from hashlib import md5 FILE_DIR = os.path.dirname(__file__) BASE_DIR = os.path.dirname(os.path.dirname(__file__)) home = os.path.join(BASE_DIR, "E:/data/upload") # 定义一个函数,计算进度条 def bar(num=1, sum=100): rate = float(num) / float(sum) rate_num = int(rate * 100) temp = '\r%d %%' % rate_num sys.stdout.write(temp) def md5_file(name): m = md5() a_file = open(name, 'rb') #需要使用二进制格式读取文件内容 m.update(a_file.read()) a_file.close() return m.hexdigest() def upload_client(local_path, depth, dateTime, conllection_code): global file_path while True: file_byte_size = os.stat(local_path).st_size # 获取文件的大小 file_name = os.path.basename(local_path) # 设置文件名 md5 = md5_file(local_path) has_sent = 0 file_obj = open(local_path, 'rb') # 对文件进行读操作 file_obj.seek(has_sent) # 调整指针 if depth == 'year': file_path = file_store_path_year(dateTime, home, conllection_code) if not os.path.exists(file_path): os.makedirs(file_path) if depth == 'month': file_path = file_store_path_month(dateTime, home, conllection_code) if not os.path.exists(file_path): os.makedirs(file_path) if depth == 'day': file_path = file_store_path_day(dateTime, home, conllection_code) if not os.path.exists(file_path): os.makedirs(file_path) path = os.path.join(file_path, file_name) has_received = 0 # 首先判断该路径下是否已存在文件 if os.path.exists(path): f = open(path, 'wb') else: f = open(path, 'wb') while has_sent < file_byte_size: # 读出数据 data = file_obj.read(1024) try: # 写入数据 f.write(data) has_received += len(data) if not data: raise Exception except Exception: flag = False break has_sent += len(data) bar(has_sent, file_byte_size) # 进度条 print("文件上传成功!") file_obj.close() f.close() file_dict = {'fileName': file_name, 'md5': md5, 'file_size': file_byte_size, 'file_path': file_path, 'type': 'ok'} return file_dict