上传文件的四种方法

(一)Channel管道

    public static void main(String[] args) throws IOException {
   
        long start = System.currentTimeMillis();
        FileChannel inChannel = FileChannel.open(Paths.get("D:" + File.separator + "upload.zip"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("D:" + File.separator + "upload3.zip"),
                StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);

        //内存映射文件
        MappedByteBuffer inMappedBuf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer outMappedBuf = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

        //直接对缓冲区进行数据的读写操作
        byte[] dst = new byte[inMappedBuf.limit()];
        inMappedBuf.get(dst);
        outMappedBuf.put(dst);
        inChannel.close();
        outChannel.close();

        long end = System.currentTimeMillis()

你可能感兴趣的:(代码)