java 文件分片

這是看的原文博客

最近 老大 突然 讓我去看度小麥的api 說是可能要對接,但那裏面的要求傳入的音頻是要求分片的,按照4M分片。於是乎,我就去找了一下java 分片的博客原樣寫了一個demo,額 做了一丟丟的修改吧。按理說 我是不想放原創的,但是呢 水一篇麼

這是一個這個 參數類

public class SplitFileParam {
    public static String file="C:\\Users\\dfsj17052702\\Desktop\\photo/1.mp3"; //文件的路徑
    public static String outfile="C:\\Users\\dfsj17052702\\Desktop\\photo/out.mp3"; //文件的路徑
    public static int count=10;   //將文件切割成多少份  這個 不是符合我的要求 不予使用
    public static int maxSize=4*1024*1024;   //將文件切割成多大
    

}

下面是我的demo

public class SplitFile {
 
    public static void main(String[] args) throws IOException {
        getSplitFile();
        String file = SplitFileParam.file; //文件的路徑
        RandomAccessFile raf = null;
        raf = new RandomAccessFile(new File(file), "r");
        long length = raf.length();//文件的總長度
        long maxSize = SplitFileParam.maxSize;//文件切片後的長度
        long count = length/maxSize; //文件分割的份數
        merge(SplitFileParam.outfile,SplitFileParam.file,count);
    }
 
    /**
     * 文件分割方法
     */
    public static void getSplitFile() {
        String file = SplitFileParam.file; //文件的路徑
 
        RandomAccessFile raf = null;
        try {
            //獲取目標文件 預分配文件所佔的空間 在磁盤中創建一個指定大小的文件   r 是隻讀
            raf = new RandomAccessFile(new File(file), "r");
            long length = raf.length();//文件的總長度
            long maxSize = SplitFileParam.maxSize;//文件切片後的長度
//            long count = length/maxSize; //文件分割的份數
            long count = length/maxSize; //文件分割的份數
            long offSet = 0L;//初始化偏移量
            for (long i = 0; i < count; i++) { //最後一片單獨處理  我這樣計算出來的 count  本身  就是 吧 最後一片 除掉的 
                long begin = offSet;
                long end = (i + 1) * maxSize;
//                offSet = writeFile(file, begin, end, i);
                offSet = getWrite(file, i, begin, end);
            }
            if (length - offSet > 0) {
                getWrite(file, count, offSet, length);
            }
 
        } catch (FileNotFoundException e) {
            System.out.println("沒有找到文件");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 指定文件每一份的邊界,寫入不同文件中
     * @param file 源文件
     * @param i 源文件的順序標識
     * @param begin 開始指針的位置
     * @param end 結束指針的位置
     * @return long
     */
    public static long getWrite(String file,long i,long begin,long end){
        String a=file.split(".mp3")[0];
        long endPointer = 0L;
        byte[] data = null;
        try {
            //申明文件切割後的文件磁盤
            RandomAccessFile in = new RandomAccessFile(new File(file), "r");
            //定義一個可讀,可寫的文件並且後綴名爲.tmp的二進制文件
            RandomAccessFile out = new RandomAccessFile(new File(a + "_" + i + ".tmp"), "rw");
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            
            //申明具體每一文件的字節數組
            byte[] b = new byte[1024];
            int n = 0;
            //從指定位置讀取文件字節流
            in.seek(begin);
            //判斷文件流讀取的邊界
            while(in.getFilePointer() < end && (n = in.read(b)) != -1){
                if(in.getFilePointer() > end && in.getFilePointer() < 615420 ) {
                    System.out.println(in.getFilePointer());
                }
                //從指定每一份文件的範圍,寫入不同的文件
//                baos.write(b, 0, n);
                out.write(b, 0, n);
                
            }
            data = baos.toByteArray();
            String str = new String(data,"UTF-8");
            //定義當前讀取文件的指針
            endPointer = in.getFilePointer();
            //關閉輸入流
            in.close();
            //關閉輸出流
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return endPointer;
    }
    /**
     * 文件合併
     * @param file 指定合併文件
     * @param tempFile 分割前的文件名
     * @param count 文件個數
     */
    public static void merge(String file,String tempFile,long count) {
        String a=tempFile.split(".mp3")[0];
        RandomAccessFile raf = null;
        try {
            //申明隨機讀取文件RandomAccessFile
            raf = new RandomAccessFile(new File(file), "rw");
            //開始合併文件,對應切片的二進制文件
            for (int i = 0; i < count+1; i++) {
                //讀取切片文件
                RandomAccessFile reader = new RandomAccessFile(new File(a + "_" + i + ".tmp"), "r");
                byte[] b = new byte[1024];
                int n = 0;
                //先讀後寫
                while ((n = reader.read(b)) != -1) {//讀
                    raf.write(b, 0, n);//寫
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

看着 好像 low了一點 哈哈 充滿了 懶人 與 彩筆的氣息

在用這個 demo的時候 我發現 一個 問題
用他原來的demo時候 生成的文件,第一個 每次 都多1kb。
如上圖
搞得我一臉懵逼,按理來說,他要是有問題 不應該 都多1kb麼。
最後我靈機一動 咳咳
在這裏插入圖片描述
在這裏插入圖片描述
改成了這樣,就好了 捂臉 表示有大佬可以解釋一波的麼

我感覺 是因爲 是因爲判斷了 = 所以 第一個的時候 就會 恰好 和 end的值相等,就會多讀進去1kb,對 還因爲 我byte數組是定義的 1kb 容量所以會造成這個原因
嗯 想想 我解釋的 還是 很合情合理的

感覺 好像 執行速度 比較慢,這個 可以 用到生產麼? 如有大哥做過,並查到了這篇文章,看到了這句話,有比較好的方案 ,請務必留言給我,亦或者 評論留下 播客地址也可。

另附 幾個可能會用到的播客 不知 是否 好用 可用

java大文件分塊上傳分享:

JAVA中獲取文件MD5值的四種方法

Java把一個文件轉化爲byte字節數組

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章