普寧17網(wǎng)站一起做網(wǎng)店學大教育培訓機構怎么樣
文件的分片上傳
格外功能是:秒傳,斷點續(xù)傳。
今天最慘,上午找bug,下午一直在修改,晚上腦子what了,混亂的很,數(shù)據(jù)表之間的邏輯不清晰,導致我傳值,還有操作數(shù)據(jù)庫一直有問題,這里最大的問題就是文件唯一了,然后要單獨建立一個file表,不與班級,有關,班級文件表可以單獨建立,可是當時想著把正在上傳的和已經(jīng)上傳的建立了兩張表,現(xiàn)在想想都后悔,如果要修改,好多要改的,所以就將就使用兩張表吧。
8/4:今天一上午都在檢查bug都沒解決,現(xiàn)在終于解決:
1:由于我的socket是new的一個,導致我發(fā)請求在同一個返回的請求類型(當我得到服務器可以上傳文件信息時,
???我立刻上傳文件所有分片,當我的分片發(fā)完,后立刻發(fā)文件上傳完成請求(這時候又是發(fā)送在原來的socket里面)
???當服務端接受到文件上傳完成,立刻合并分片,會導致導致我new的哪個socket里的請求,上傳文件程序還沒有
???完成,導致我的文件分片列表files里為null
2:在合并文件時,我直接對文件分片進行Array.sort(),這樣導致文件合片順序錯誤,part11?<?part12?<?part2
?
文件分片代碼:
public static void splitFile(File file,String md5) throws IOException {int partCounter = 0;byte[] buffer = new byte[CHUNK_SIZE];String fileName = file.getName();//創(chuàng)建目錄File dir = new File("src/file/" + md5);if (!dir.exists()) {boolean created = dir.mkdirs(); // 創(chuàng)建目錄及必要的父目錄if (!created) {throw new IOException("Failed to create directory: " + dir.getAbsolutePath());}}try (FileInputStream fis = new FileInputStream(file);BufferedInputStream bis = new BufferedInputStream(fis)) {int bytesAmount = 0;while ((bytesAmount = bis.read(buffer)) > 0) {String filePartName = String.format("%s.part%d", md5, partCounter++);File newFile = new File(dir, filePartName);try (FileOutputStream out = new FileOutputStream(newFile)) {out.write(buffer, 0, bytesAmount);}}}
?這樣就可以創(chuàng)建很多分片,然后可以分部上傳。
在我上傳文件時,是不接受服務端返回的消息,直接發(fā)送所有消息的,有問題。后面再修改。
這里暫停上傳時,要修改一下。
在上傳完成之后
服務端進行文件合并
public static File fileMerge(MyLargerFile myLargerFile) throws IOException {File newFile = new File("src/resource/files/" +myLargerFile.getMd5()+"."+myLargerFile.getOriginalName());File[] files = new File("src/resource/file/" +myLargerFile.getMd5()+"part").listFiles();Arrays.sort(files, (f1, f2) -> {// 分割文件名,獲取分片編號部分String[] parts1 = f1.getName().split("\\.part");String[] parts2 = f2.getName().split("\\.part");// 提取并解析分片編號int part1 = Integer.parseInt(parts1[1]);int part2 = Integer.parseInt(parts2[1]);// 比較分片編號return Integer.compare(part1, part2);});try(FileOutputStream out = new FileOutputStream(newFile)) {for (File file1 : files) {System.out.println(file1.getName());try (FileInputStream fis = new FileInputStream(file1);BufferedInputStream bis = new BufferedInputStream(fis)){int bytesAmount = 0;byte[] buffer = new byte[CHUNK_SIZE];while ((bytesAmount = bis.read(buffer)) != -1) {out.write(buffer, 0, bytesAmount);}}}}for (File file : files){deleteFile(file);}deleteFile(new File("src/resource/file/" +myLargerFile.getMd5()+"part"));return newFile;}private static void deleteFile(File file){if (!file.delete()) {System.out.println("不能刪除該文件: " + file.getName());}}
?混亂點
在這里表有點多,就導致邏輯有點混亂,這里應該先要建立文件上傳記錄,然后上傳完成后修改數(shù)據(jù),還有混亂的就是數(shù)據(jù)的傳遞,導致客戶端和服務端的賦值,處理數(shù)據(jù)出現(xiàn)問題,這個特別嚴重,主要還是因為傳遞的參數(shù)過多,導致混淆。
今天還有一個坑就是進行合并文件時,進行文件的排序
Arrays.sort(files, (f1, f2) -> {// 分割文件名,獲取分片編號部分String[] parts1 = f1.getName().split("\\.part");String[] parts2 = f2.getName().split("\\.part");// 提取并解析分片編號int part1 = Integer.parseInt(parts1[1]);int part2 = Integer.parseInt(parts2[1]);// 比較分片編號return Integer.compare(part1, part2); });