首页 > 作文

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

更新时间:2023-04-04 02:56:14 阅读: 评论:0

项目编号:bs-pt-032

本项目基于springboot开发实现,前端采用bootstrap开发实现老年抑郁,系统功能完整,交互性好,模仿百度网盘实现相关功能,比较适合做毕业设计使用,创意性强。

开发工具为idea或eclip,数据库采用mysql数据库。

系统部分功能展示如下:

http://localhost:8080/tologin admin / 123456

登陆页面:

主页

对应本地磁盘存储目录:

分享网盘资料

根据提取码下载相关资料

下载

重命名文件或文件夹

文件上传

新建文件夹

上传音乐文件后可以一键自动播放

以上是本系统的部分展示功能,可以做为毕业设计使用。

部分代码实现如下:

package com.bjpowernode.pan.rvice.impl;import com.bjpowernode.pan.dao.model.linkcret;import com.bjpowernode.pan.model.filemsg;import com.bjpowernode.pan.rvice.ifilervice;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.rvice;import org.springframework.web.multipart.multipartfile;import java.io.*;import java.nio.channels.filechannel;import java.text.simpledateformat;import java.util.*;/***指南针毕设*/@rvicepublic class filerviceimpl implements ifilervice {public static string filerootpath;public static string temppath; //分块文件临时存储地址// 自定义密钥static private string key;@autowiredsaverviceimpl savervice;@autowiredlinkcretrviceimpl linkcretrvice;private logger logger = loggerfactory.getlogger(this.getclass());@value("${temppath}")public void ttemppath(string temppath) {filerviceimpl.temppath = temppath;}@value("${filerootpath}")public void tfilerootpath(string filerootpath) {filerviceimpl.filerootpath = filerootpath;}@value("${key}")public void tkey(string key) {filerviceimpl.key = key;}@overridepublic boolean upload(multipartfile file, string urname, string path) {boolean b = fal;// 服务器上传的文件所在路径string savefilepath = filerootpath + urname + "/" + path;logger.warn("1 savefilepath:" + savefilepath);// 判断文件夹是否存在-建立文件夹file filepathdir = new file(savefilepath);if (!filepathdir.exists()) {filepathdir.mkdir();}// 获取上传文件的原名 例464e7a80_710229096@qq.com.zipstring savefilename = file.getoriginalfilename();// 上传文件到-磁盘try {fileutils.copyinputstreamtofile(file.getinputstream(), new file(savefilepath, savefilename));b = true;} catch (exception e) {logger.error("exception:", e);return fal;}return b;}@overridepublic string download(string filename, string urname, string path) {// 服务器下载的文件所在的本地路径的文件夹string savefilepath = filerootpath + urname + "/" + 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;}@overridepublic list<filemsg> urfilelist(string urname, string path) {logger.warn("执行urfilelist函数!");list<filemsg> filemsglist = new arraylist<>();// 拉取文件列表-本地磁盘string websavefilepath = filerootpath + urname + "/" + 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());// 赋值到jsonfilemsg.tname(name);filemsg.tlink(link);filemsg.tsize(size);filemsg.ttime(lastmodtime);if (fileutil.ismp4(name)) {filemsg.ttype("mp4");} el if (fileutil.isvideo(name)) {filemsg.ttype("video");} el {filemsg.ttype("file");}filemsglist.add(filemsg);} el {filemsg filemsg = new filemsg();string link = file.tostring().replace("\\", "/");string[] namearr = link.split("/");string name = namearr[namearr.length - 1];string dirpath = link.replace(filerootpath + urname, "");if (!name.equals("uricon")) {filemsg.tname(name);filemsg.tsize("directory");filemsg.ttype("dir");filemsg.tlink(dirpath);simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");string lastmodtime = formatter.format(file.lastmodified());filem莫娣sg.ttime(lastmodtime);filemsglist.add(filemsg);}}}//排序listutil.listsort(filemsglist);return filemsglist;}/*** 展示path目录下的全部文件信息** @param path 文件完全路径* @param urname 用户名* @return filemsg list*/@overridepublic list<filemsg> list(string path, string urname) {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, urname, filerootpath, "/data/"));}// 排序规则:文件夹在前,文件在后,更新时间最近的在前listutil.listsort(filemsglist);return filemsglist;}@overridepublic boolean[] urfiledelete(string filename, string urname, string path) {//解析filename: 以$$符号分割string[] filenames = null;if (filename.contains("$$")) {filenames = filename.split("\\$\\$");} el {filenames = new string[1];filenames[0] = filename;}boolean[] b = new boolean[filenames.length];for (int i = 0; i < filenames.length; i++) {// 删除-本地文件string savefilepath = filerootpath + urname + "/" + path;file file = new file(savefilepath);file[] listfiles = file.listfiles();boolean b1 = fal;//判断是否是文件夹if (filename.equals("@dir@")) {//是文件夹b1 = fileutil.delete(savefilepath);} el {b1 = fileutil.delete(savefilepath + "/" + filenames[i]);}//                if (!b1){//                    filesave filesave=savervice.findfilesav胰高血糖素ebyurnameandfilename(urname,//                    filenames[i]);//                    savervice.delete(filesave);//                    b1=true;//                }b[i] = b1;}return b;}@overridepublic boolean urfilerename(string oldname, string newname, string urname, string path) {// 重命名-本地磁盘文件string oldnamewithpath;string newnamewithpath;if ("@dir@".equals(oldname)) {oldnamewithpath = stringutil.stringslashtoone(filerootpath + urname + "/" + path);newnamewithpath =oldnamewithpath.substring(0, (int) stringutil.getfilesuffix(oldnamewithpath, true, "/")) + "/" + newname;newnamewithpath = stringutil.stringslashtoone(newnamewithpath);} el {oldnamewithpath = stringutil.stringslashtoone(filerootpath + urname + "/" + path + "/" + oldname);newnamewithpath = stringutil.stringslashtoone(filerootpath + urname + "/" + path + "/" + newname);}return fileutil.renamefile(oldnamewithpath, newnamewithpath);}@overridepublic boolean urdircreate(string dirname, string path) {file file = new file(path + "/" + dirname);return file.mkdir();}@overridepublic 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";}@overridepublic 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("/");linkcret linkcret = linkcretrvice.findlinkcretbycretlink(code);string[] locallink = linkcret.getlocallink().split("/");string urname = locallink[3];//            string urname = arr[0];string filename = arr[arr.length - 1];arr[arr.length - 1] = "";//            string path = stringutils.join(arr, "/");string path = urname + "/";if (locallink.length > 5) {for (int k = 4; k < locallink.length - 1; k++) {path = path + locallink[k] + "/";}}logger.warn("0 urname:" + urname);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";}}@overridepublic boolean urfiledirmove(string filename, string oldpath, string newpath, string urname) {// 移动-本地磁盘文件string savefilepath = filerootpath + urname + "/";string lfilename = ("@dir@".equals(filename) ? "" : "/" + filename);string oldnamewithpath = stringutil.stringslashtoone(savefilepath + oldpath + lfilename);string tmpnewfilename = "@dir@".equals(filename) ?(string) stringutil.getfilesuffix(oldnamewithpath, fal, "/", fal) : "";string newnamewithpath = stringutil.stringslashtoone(savefilepath + newpath + lfilename + tmpnewfilename);return fileutil.renamefile(oldnamewithpath, newnamewithpath);}@overridepublic list<filemsg> arch(string key, string urname, string path) {list<filemsg> filemsglist = new arraylist<>();// 拉取文件列表-本地磁盘string websavefilepath = filerootpath + urname + "/" + path;file files = new file(websavefilepath);if (!files.exists()) {files.mkdir();}//            file[] templist = files.listfiles();list<file> templist = new arraylist<>();templist = archfilebykey.archfile(websavefilepath, key, fal, templist);for (int i = 0; i < templist.size(); i++) {if (templist.get(i).isfile()) {//      黄品冠好听的歌          logger.warn("用户:" + urname + " 文件:" + 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());// 赋值到jsonfilemsg.tname(name);filemsg.tlink(link);filemsg.tsize(size);filemsg.ttime(lastmodtime);filemsglist.add(filemsg);} el {filemsg filemsg = new filemsg();string link = templist.get(i).tostring().replace("\\", "/");string[] namearr = link.split("/");string name = namearr[namearr.length - 1];if (!name.equals("uricon")) {filemsg.tlink(link);filemsg.tname(name);filemsg.tsize("directory");simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");string lastmodtime = formatter.format(templist.get(i).lastmodified());filemsg.ttime(lastmodtime);filemsglist.add(filemsg);}}}return filemsglist;}@overridepublic boolean merge(string filename, string urname, string path) throws interruptedexception {boolean b = fal;string savepath = filerootpath + urname + "/" + path;file savedir = new file(savepath);if (!savedir.exists()) {savedir.mkdirs();}string tempdirpath = fileutil.gettempdir(temppath, urname, filename);file tempdir = new file(tempdirpath);// 获得分片文件列表file[] filearray = tempdir.listfiles(new filefilter() {// 只需要文件@overridepublic boolean accept(file pathname) {if (pathname.isdirectory()) {return fal;} el {return true;}}});//        logger.warn("【要合成的文件有】:"+filearray);//       while (filearray==null){//       }// 转成集合进行排序后合并文件list<file> filelist = new arraylist<file>(arrays.aslist(filearray));collections.sort(filelist, new comparator<file>() {// 按文件名升序排列@overridepublic int compare(file o1, file o2) {if (integer.parint(o1.getname()) < integer.parint(o2.getname())) {return -1;} el {return 1;}}});// 目标文件file outfile = new file(savepath + file.parator + filename);try {outfile.createnewfile();} catch (ioexception e) {b = fal;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.clo();file1.delete();}outchannel.clo();} catch (filenotfoundexception e) {b = fal;logger.warn("合并分片文件出错:" + e.getmessage());logger.error("exception:", e);} catch (ioexception e) {b = fal;logger.warn("合并分片文件出错:" + e.getmessage());logger.error("exception:", e);}// 删除临时文件夹 根目录/temp/urname/filenamef情情五月ile tempfiledir = new file(temppath + file.parator + urname + file.parator + filename);fileutil.deletedir(tempfiledir);return b;}//locallink是原始文件路径,path:存取路径@overridepublic boolean copyfiletomypan(string urname, string locallink, string path) {boolean b = fal;//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 + urname + "/" + 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;} el {//todologger.warn("存在同名文件");b = fal;}} catch (ioexception e) {logger.error("exception:", e);return fal;}logger.warn("copyfiletomypan() result:{}", b);return b;}}

到此这篇关于自己动手用springboot实现仿百度网盘的实践的文章就介绍到这了,更多相关springboot仿百度网盘内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 02:56:08,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/f181e3a476d6a0fdb4c49989290cb5ce.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:自己动手用Springboot实现仿百度网盘的实践.doc

本文 PDF 下载地址:自己动手用Springboot实现仿百度网盘的实践.pdf

标签:文件   文件夹   路径   磁盘
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图