使用NIO上传与合并文件(分片上传结果集)

上传方法

/**
     * 写文件
     * @param bytes 文件内容
     * @param fileName 文件名称
     * @param filePath 文件路径
     */
    public static void writeFile(byte[] bytes, String fileName, String filePath){
        log.info("method:{},fileName:{}","writeFile", fileName);
        FileOutputStream fileOutputStream = null;
        FileChannel writeChannel = null;
        try {
            fileOutputStream = new FileOutputStream(new File(filePath + File.separator + new String(fileName.getBytes(StandardCharsets.UTF_8))));
            writeChannel = fileOutputStream.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
            writeChannel.write(byteBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fileOutputStream != null){
                    fileOutputStream.close();
                    if(writeChannel != null){
                        writeChannel.close();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

合并方法

/**
     * 合并文件 (删除子文件)
     * @param subFiles 子文件
     * @param file 合并后的文件
     */
    public static void mergeFiles(List subFiles, String file){
        log.info("method:{},file:{}","subFiles", file);
        if(CollectionUtils.isEmpty(subFiles) || StringUtils.isEmpty(file)){
            return;
        }
        FileChannel opsChannel = null;
        try {
            RandomAccessFile ops = new RandomAccessFile(file, "rw");
            opsChannel = ops.getChannel();
            int count =0;
            if(subFiles.size() > 8){
                while (count*8 < subFiles.size()){
                    int lastLength = Math.min(8 * (count + 1), subFiles.size());
                    List subFilesTemp = subFiles.subList(count*8, lastLength);
                    MappedByteBuffer[] buffers = new MappedByteBuffer[subFilesTemp.size()];
                    for (int i=0; i< subFilesTemp.size(); i++) {
                        RandomAccessFile raf = new RandomAccessFile(subFilesTemp.get(i), "r");
                        FileChannel channel = raf.getChannel();
                        buffers[i] = channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
                        channel.close();
                    }
                    opsChannel.write(buffers);
                    for (MappedByteBuffer buffer : buffers) {
                        unmap(buffer);
                    }
                    buffers = null;
                    count++;
                }
            }else{
                MappedByteBuffer[] buffers = new MappedByteBuffer[subFiles.size()];
                for (int i=0; i< subFiles.size(); i++) {
                    RandomAccessFile raf = new RandomAccessFile(subFiles.get(i), "r");
                    FileChannel channel = raf.getChannel();
                    buffers[i] = channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
                    channel.close();
                }
                opsChannel.write(buffers);
                for (MappedByteBuffer buffer : buffers) {
                    unmap(buffer);
                }
                buffers = null;
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            try {
                if(opsChannel != null && opsChannel.isOpen()){
                    opsChannel.close();
                }
                for (String subFile : subFiles) {
                    remove(subFile);
                }
                System.gc();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }

    }

/**
     * 删除文件
     * @param filePath 文件路径
     */
    public static void remove(String filePath){
        log.info("method:{},filePath:{}","remove", filePath);
        File file = new File(filePath);
        if(file.exists()){
            file.delete();
        }
    }

    private static void unmap(MappedByteBuffer var0){
        Method m = null;
        try {
            m = FileChannelImpl.class.getDeclaredMethod("unmap",
                    MappedByteBuffer.class);
            m.setAccessible(true);
            m.invoke(FileChannelImpl.class, var0);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(使用NIO上传与合并文件(分片上传结果集))