我是靠谱客的博主 故意黑夜,这篇文章主要介绍自己动手用Springboot实现仿百度网盘的实践,现在分享给大家,希望可以做个参考。

项目编号:BS-PT-032

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

开发工具为IDEA或ECLIPSE,数据库采用MYSQL数据库。

系统部分功能展示如下:

http://localhost:8080/toLogin admin / 123456

登陆页面:

主页

对应本地磁盘存储目录:

分享网盘资料

根据提取码下载相关资料

下载

重命名文件或文件夹

文件上传

新建文件夹

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

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

部分代码实现如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
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仿百度网盘内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是故意黑夜最近收集整理的关于自己动手用Springboot实现仿百度网盘的实践的全部内容,更多相关自己动手用Springboot实现仿百度网盘内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(111)

评论列表共有 0 条评论

立即
投稿
返回
顶部