我正在尝试使用JCIFS将一些远程文件复制到Java中的本地驱动器.远程计算机位于域内.本地计算机不在域中.
以下代码有效,但它真的很慢(700Kb为2分钟......我有很多Mb ......):
SmbFile remoteFile = new SmbFile("smb://...")
OutputStream os = new FileOutputStream("/path/to/local/file");
InputStream is = remoteFile.getInputStream();
int ch;
while ((ch = is.read()) != -1) {
os.write(ch);
}
os.close();
is.close();
Run Code Online (Sandbox Code Playgroud)
我想我可以使用SmbFile.copyTo(),但我不知道如何访问本地文件.如果我写下面的内容,我会收到连接错误:
localfile = new SmbFile("file:///path/to/localfile")
Run Code Online (Sandbox Code Playgroud)
小智 5
你只需要制作更大的缓冲区:
SmbFile remoteFile = new SmbFile("smb://...")
OutputStream os = new FileOutputStream("/path/to/local/file");
InputStream is = remoteFile.getInputStream();
int bufferSize = 5096;
byte[] b = new byte[bufferSize];
int noOfBytes = 0;
while( (noOfBytes = is.read(b)) != -1 )
{
os.write(b, 0, noOfBytes);
}
os.close();
is.close();
Run Code Online (Sandbox Code Playgroud)
这里使用提到的代码对文件23 Mb进行了一些测试.
bufferSize = 1024经过时间:10.9587606066秒
bufferSize = 4096经过时间:5.6239662951秒
bufferSize = 5096经过时间:5.0798761245秒
bufferSize = 5096经过时间:4.879439883秒
bufferSize = 10240经过时间:4.0192989201秒
bufferSize = 50240经过时间:3.8876541543秒
bufferSize = 100240经过时间:3.742167582秒
| 归档时间: |
|
| 查看次数: |
17979 次 |
| 最近记录: |