如何在android中快速将大视频文件上传到服务器

时间:2017-04-28 04:36:19

标签: android

您好我正在使用Volley Multi-part Api将大型视频文件上传到服务器,但上传到服务器需要很长时间

将视频文件拆分并发送到服务器是否更好?如果它更好,请提供代码我该怎么做,如果不是什么是快速上传大视频文件到服务器的最佳方式?

1 个答案:

答案 0 :(得分:3)

将文件拆分为多个部分(块):

public static List<File> splitFile(File f) throws IOException {
    int partCounter = 1;
    List<File> result = new ArrayList<>();
    int sizeOfFiles = 1024 * 1024;// 1MB
    byte[] buffer = new byte[sizeOfFiles]; // create a buffer of bytes sized as the one chunk size

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
    String name = f.getName();

    int tmp = 0;
    while ((tmp = bis.read(buffer)) > 0) {
        File newFile = new File(f.getParent(), name + "." + String.format("%03d", partCounter++)); // naming files as <inputFileName>.001, <inputFileName>.002, ...
        FileOutputStream out = new FileOutputStream(newFile);
        out.write(buffer, 0, tmp);//tmp is chunk size. Need it for the last chunk, which could be less then 1 mb.
        result.add(newFile);
    }
    return result;
}

此方法会将文件拆分为大小为1MB的块(不包括最后一个块)。在单词之后,您也可以将所有这些块发送到服务器。

此外,如果您需要合并这些文件:

public static void mergeFiles(List<File> files, File into)
        throws IOException {
   BufferedOutputStream mergingStream = new BufferedOutputStream(new FileOutputStream(into))
    for (File f : files) {
        InputStream is = new FileInputStream(f);
        Files.copy(is, mergingStream);
        is.close();
    }
    mergingStream.close();
}

以防你的服务器是Java还是