Java 使用jcifs复制文件需要很长时间
我正在将远程文件从windows共享文件夹复制到linux计算机。复制要花很长时间。320 MB中只有200 Kb在10小时内复制 以下是我的代码片段:Java 使用jcifs复制文件需要很长时间,java,jcifs,Java,Jcifs,我正在将远程文件从windows共享文件夹复制到linux计算机。复制要花很长时间。320 MB中只有200 Kb在10小时内复制 以下是我的代码片段: try { String user = "xxxx"; String pass ="yyyy"; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", user, pass); String sha
try {
String user = "xxxx";
String pass ="yyyy";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",
user, pass);
String sharepath ="smb://aa.bb.com/root/Test/Access.mdb";
SmbFile remoteFile = new SmbFile (sharepath, auth);
OutputStream os = new FileOutputStream("/home/test/Access.mdb");
InputStream is = remoteFile.getInputStream();
int ch;
while ((ch = is.read()) != -1) {
os.write(ch);
}
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
如何减少复制时间?使用缓冲时,从/到大多数资源的流式传输速度更快。 为此,请使用
BufferedInputStream
和BufferedOutput Stream
:
OutputStream os = new BufferedOutputStream(new FileOutputStream("/home/test/Access.mdb"));
InputStream is = new BufferedInputStream(remoteFile.getInputStream());
在关闭输出流之前,始终flush()
,当任何包装流使用缓冲时,这一点至关重要。关闭而不刷新将导致数据丢失
os.flush();
os.close();
is.close();
如果复制200KB需要10个小时,则说明您的设置存在严重问题。可能存在网络问题,或者您的代码和安装程序可能会触发jcifs或Windows中的错误。启用所有日志记录,并可能使用调试器和配置文件查看时间花在哪里
作为一种快速解决方案,您可以考虑使用SSH或RSyc等不同的协议与SSH。
或者看看像这样的远程文件系统(不过,在您的情况下可能有些过分)