我在写一个java rest服务来支持大文件的部分并行上传。我正在将这些部分写入单独的文件中,并使用文件通道合并它们。我有一个在Golang中实现的示例,它执行相同的操作,但是它在合并这些部分时不需要时间。当我使用文件通道或从一个流中读取并写入最终文件时,需要很长时间。我认为这种差异在于,Golang有能力将数据保存在磁盘上,并通过不实际移动数据来合并它们。有什么办法可以在java中做同样的事情吗? 这里是我的代码,通过这种方法对所有部件合并部分,我循环:在java中更快地合并大文件部分
private void mergeFileUsingChannel(String destinationPath, String sourcePath, long partSize, long offset) throws Exception{
FileChannel outputChannel = null;
FileChannel inputChannel = null;
try{
outputChannel = new FileOutputStream(new File(destinationPath)).getChannel();
outputChannel.position(offset);
inputChannel = new FileInputStream(new File(sourcePath)).getChannel();
inputChannel.transferTo(0, partSize, outputChannel);
}catch(Exception e){
e.printStackTrace();
}
finally{
if(inputChannel != null)
inputChannel.close();
if(outputChannel != null){
outputChannel.close();
}
}
}
操作系统和文件系统类型可能与此问题有关吗? –
服务器是fedora 24.我不确定文件系统。我正在将文件传输到HDD,SSD和网络位置。 –