自己动手用Springboot实现仿百度网盘的实践
程序员文章站
2022-06-18 19:18:15
项目编号:bs-pt-032本项目基于springboot开发实现,前端采用bootstrap开发实现,系统功能完整,交互性好,模仿百度网盘实现相关功能,比较适合做毕业设计使用,创意性强。开发工具为i...
项目编号:bs-pt-032
本项目基于springboot开发实现,前端采用bootstrap开发实现,系统功能完整,交互性好,模仿百度网盘实现相关功能,比较适合做毕业设计使用,创意性强。
开发工具为idea或eclipse,数据库采用mysql数据库。
系统部分功能展示如下:
http://localhost:8080/tologin admin / 123456
登陆页面:
主页
对应本地磁盘存储目录:
分享网盘资料
根据提取码下载相关资料
下载
重命名文件或文件夹
文件上传
新建文件夹
上传音乐文件后可以一键自动播放
以上是本系统的部分展示功能,可以做为毕业设计使用。
部分代码实现如下:
package com.bjpowernode.pan.service.impl; import com.bjpowernode.pan.dao.model.linksecret; import com.bjpowernode.pan.model.filemsg; import com.bjpowernode.pan.service.ifileservice; import com.bjpowernode.pan.util.*; import org.apache.commons.io.fileutils; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.service; import org.springframework.web.multipart.multipartfile; import java.io.*; import java.nio.channels.filechannel; import java.text.simpledateformat; import java.util.*; /** *指南针毕设 */ @service public class fileserviceimpl implements ifileservice { public static string filerootpath; public static string temppath; //分块文件临时存储地址 // 自定义密钥 static private string key; @autowired saveserviceimpl saveservice; @autowired linksecretserviceimpl linksecretservice; private logger logger = loggerfactory.getlogger(this.getclass()); @value("${temppath}") public void settemppath(string temppath) { fileserviceimpl.temppath = temppath; } @value("${filerootpath}") public void setfilerootpath(string filerootpath) { fileserviceimpl.filerootpath = filerootpath; } @value("${key}") public void setkey(string key) { fileserviceimpl.key = key; } @override 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(); } // 获取上传文件的原名 例464e7a80_710229096@qq.com.zip 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; } @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<filemsg> userfilelist(string username, string path) { logger.warn("执行userfilelist函数!"); list<filemsg> 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 filemsglist; } /** * 展示path目录下的全部文件信息 * * @param path 文件完全路径 * @param username 用户名 * @return filemsg list */ @override public list<filemsg> list(string path, string username) { list<filemsg> filemsglist = new arraylist<>(); file files = new file(path); if (!files.exists()) { return filemsglist; } file[] templist = files.listfiles(); if (templist == null) { return filemsglist; } // 遍历每个文件转json对象 for (file file : templist) { filemsglist.add(fileutil.filetofilemsg(file, username, filerootpath, "/data/")); } // 排序规则:文件夹在前,文件在后,更新时间最近的在前 listutil.listsort(filemsglist); return filemsglist; } @override public boolean[] userfiledelete(string filename, string username, string path) { //解析filename: 以$$符号分割 string[] filenames = null; if (filename.contains("$$")) { filenames = filename.split("\\$\\$"); } else { filenames = new string[1]; filenames[0] = filename; } boolean[] b = new boolean[filenames.length]; for (int i = 0; i < filenames.length; i++) { // 删除-本地文件 string savefilepath = filerootpath + username + "/" + path; file file = new file(savefilepath); file[] listfiles = file.listfiles(); boolean b1 = false; //判断是否是文件夹 if (filename.equals("@dir@")) { //是文件夹 b1 = fileutil.delete(savefilepath); } else { b1 = fileutil.delete(savefilepath + "/" + filenames[i]); } // if (!b1){ // filesave filesave=saveservice.findfilesavebyusernameandfilename(username, // filenames[i]); // saveservice.delete(filesave); // b1=true; // } b[i] = b1; } return b; } @override public boolean userfilerename(string oldname, string newname, string username, string path) { // 重命名-本地磁盘文件 string oldnamewithpath; string newnamewithpath; if ("@dir@".equals(oldname)) { oldnamewithpath = stringutil.stringslashtoone(filerootpath + username + "/" + path); newnamewithpath = oldnamewithpath.substring(0, (int) stringutil.getfilesuffix(oldnamewithpath, true, "/")) + "/" + newname; newnamewithpath = stringutil.stringslashtoone(newnamewithpath); } else { oldnamewithpath = stringutil.stringslashtoone(filerootpath + username + "/" + path + "/" + oldname); newnamewithpath = stringutil.stringslashtoone(filerootpath + username + "/" + path + "/" + newname); } return fileutil.renamefile(oldnamewithpath, newnamewithpath); } @override public boolean userdircreate(string dirname, string path) { file file = new file(path + "/" + dirname); return file.mkdir(); } @override 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"; } @override public string filesharecodedecode(string code) { encryptutil des; try { des = new encryptutil(key, "utf-8"); logger.warn("00 code:" + code); string filepathandname = des.decode(code); logger.warn("00 filepathandname:" + filepathandname); string[] arr = filepathandname.split("/"); linksecret linksecret = linksecretservice.findlinksecretbysecretlink(code); string[] locallink = linksecret.getlocallink().split("/"); string username = locallink[3]; // string username = arr[0]; string filename = arr[arr.length - 1]; arr[arr.length - 1] = ""; // string path = stringutils.join(arr, "/"); string path = username + "/"; if (locallink.length > 5) { for (int k = 4; k < locallink.length - 1; k++) { path = path + locallink[k] + "/"; } } logger.warn("0 username:" + username); logger.warn("1 filepathandname:" + filepathandname); logger.warn("2 filename:" + filename); logger.warn("3 path:" + path); // 服务器下载的文件所在的本地路径的文件夹 string savefilepath = filerootpath + "share" + "/" + path; // string savefilepath = filerootpath + "/" + path; logger.warn("1 savefilepath:" + savefilepath); // 判断文件夹是否存在-建立文件夹 file filepathdir = new file(savefilepath); if (!filepathdir.exists()) { // mkdirs递归创建父目录 boolean b = filepathdir.mkdirs(); logger.warn("递归创建父目录:" + b); } savefilepath = filerootpath + "/" + path + "/" + filename; string link = savefilepath.replace(filerootpath, "/data/"); link = stringutil.stringslashtoone(link); logger.warn("4 link:" + link); // 返回下载路径 return link; } catch (exception e) { logger.error("exception:", e); return "null"; } } @override public boolean userfiledirmove(string filename, string oldpath, string newpath, string username) { // 移动-本地磁盘文件 string savefilepath = filerootpath + username + "/"; string lfilename = ("@dir@".equals(filename) ? "" : "/" + filename); string oldnamewithpath = stringutil.stringslashtoone(savefilepath + oldpath + lfilename); string tmpnewfilename = "@dir@".equals(filename) ? (string) stringutil.getfilesuffix(oldnamewithpath, false, "/", false) : ""; string newnamewithpath = stringutil.stringslashtoone(savefilepath + newpath + lfilename + tmpnewfilename); return fileutil.renamefile(oldnamewithpath, newnamewithpath); } @override public list<filemsg> search(string key, string username, string path) { list<filemsg> filemsglist = new arraylist<>(); // 拉取文件列表-本地磁盘 string websavefilepath = filerootpath + username + "/" + path; file files = new file(websavefilepath); if (!files.exists()) { files.mkdir(); } // file[] templist = files.listfiles(); list<file> templist = new arraylist<>(); templist = searchfilebykey.searchfile(websavefilepath, key, false, templist); for (int i = 0; i < templist.size(); i++) { if (templist.get(i).isfile()) { // logger.warn("用户:" + username + " 文件:" + templist[i]); filemsg filemsg = new filemsg(); // 获取文件名和下载地址 string link = templist.get(i).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(templist.get(i).length()); simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string lastmodtime = formatter.format(templist.get(i).lastmodified()); // 赋值到json filemsg.setname(name); filemsg.setlink(link); filemsg.setsize(size); filemsg.settime(lastmodtime); filemsglist.add(filemsg); } else { filemsg filemsg = new filemsg(); string link = templist.get(i).tostring().replace("\\", "/"); string[] namearr = link.split("/"); string name = namearr[namearr.length - 1]; if (!name.equals("usericon")) { filemsg.setlink(link); filemsg.setname(name); filemsg.setsize("directory"); simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string lastmodtime = formatter.format(templist.get(i).lastmodified()); filemsg.settime(lastmodtime); filemsglist.add(filemsg); } } } return filemsglist; } @override public boolean merge(string filename, string username, string path) throws interruptedexception { boolean b = false; string savepath = filerootpath + username + "/" + path; file savedir = new file(savepath); if (!savedir.exists()) { savedir.mkdirs(); } string tempdirpath = fileutil.gettempdir(temppath, username, filename); file tempdir = new file(tempdirpath); // 获得分片文件列表 file[] filearray = tempdir.listfiles(new filefilter() { // 只需要文件 @override public boolean accept(file pathname) { if (pathname.isdirectory()) { return false; } else { return true; } } }); // logger.warn("【要合成的文件有】:"+filearray); // while (filearray==null){ // } // 转成集合进行排序后合并文件 list<file> filelist = new arraylist<file>(arrays.aslist(filearray)); collections.sort(filelist, new comparator<file>() { // 按文件名升序排列 @override public int compare(file o1, file o2) { if (integer.parseint(o1.getname()) < integer.parseint(o2.getname())) { return -1; } else { return 1; } } }); // 目标文件 file outfile = new file(savepath + file.separator + filename); try { outfile.createnewfile(); } catch (ioexception e) { b = false; logger.warn("创建目标文件出错:" + e.getmessage()); logger.error("exception:", e); } // 执行合并操作 filechannel outchannel = null; filechannel inchannel; try { outchannel = new fileoutputstream(outfile).getchannel(); for (file file1 : filelist) { inchannel = new fileinputstream(file1).getchannel(); inchannel.transferto(0, inchannel.size(), outchannel); inchannel.close(); file1.delete(); } outchannel.close(); } catch (filenotfoundexception e) { b = false; logger.warn("合并分片文件出错:" + e.getmessage()); logger.error("exception:", e); } catch (ioexception e) { b = false; logger.warn("合并分片文件出错:" + e.getmessage()); logger.error("exception:", e); } // 删除临时文件夹 根目录/temp/username/filename file tempfiledir = new file(temppath + file.separator + username + file.separator + filename); fileutil.deletedir(tempfiledir); return b; } //locallink是原始文件路径,path:存取路径 @override public boolean copyfiletomypan(string username, string locallink, string path) { boolean b = false; //share文件所在的地方 logger.warn("0 locallink:" + locallink); locallink = locallink.replace("/data/", filerootpath); logger.warn("0.1 locallink2:" + locallink); file oldfile = new file(locallink); string[] msg = locallink.split("/"); string savefilename = oldfile.getname(); string savefilepath = filerootpath + username + "/" + path; logger.warn("0.2 savefilepath:" + savefilepath); file newfiledir = new file(savefilepath); if (!newfiledir.exists()) { newfiledir.mkdir(); } try { if (oldfile.exists()) { fileutils.copyinputstreamtofile(new fileinputstream(oldfile), new file(savefilepath, savefilename)); b = true; } else { //todo logger.warn("存在同名文件"); b = false; } } catch (ioexception e) { logger.error("exception:", e); return false; } logger.warn("copyfiletomypan() result:{}", b); return b; } }
到此这篇关于自己动手用springboot实现仿百度网盘的实践的文章就介绍到这了,更多相关springboot仿百度网盘内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!