gpt4 book ai didi

android - 向服务器发送 spitting/chunk 文件有什么用

转载 作者:行者123 更新时间:2023-11-29 01:10:09 24 4
gpt4 key购买 nike

我必须将大视频文件上传到服务器,但上传时间太长,所以我决定将文件拆分/分块,然后将它们发送到服务器

拆分文件后,我得到如下响应:

[ /storage/emulated/0/1493357699.mp4.001, /storage/emulated/0/1493357699.mp4.002, /storage/emulated/0/1493357699.mp4.003, /storage/emulated/0/1493357699.mp4.004, /storage/emulated/0/1493357699.mp4.005, /storage/emulated/0/1493357699.mp4.006, /storage/emulated/0/1493357699.mp4.007, /storage/emulated/0/1493357699.mp4.008 ] 

我的想法是上传spitting/chunk文件到服务器有什么用?

我的分割文件代码:

 public static List<File> splitFile(File f) {

try {

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;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}

最佳答案

我已经在我的一个项目中实现了。我看到两个主要原因:

  1. 实现多线程/多连接上传 block 。您可以同时上传多个 block 。
  2. 如果任一 block 上传失败(取决于服务器响应),则停止/恢复其余 block 的上传

关于android - 向服务器发送 spitting/chunk 文件有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43751710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com