【问题标题】:Copying files taking long time using jcifs使用 jcifs 复制文件需要很长时间
【发布时间】:2013-11-24 08:03:04
【问题描述】:

我正在将远程文件从 windows 共享文件夹复制到 linux 机器。复制需要很长时间。在 320 MB 中,仅 200 Kb 在 10 小时内复制。

这是我的代码 sn-p:

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(); 

}

如何减少复制时间?

【问题讨论】:

    标签: java jcifs


    【解决方案1】:

    如果复制 200KB 需要 10 个小时,则说明您的设置存在严重问题。可能存在网络问题,或者您的代码和设置可能会触发 jcifs 或 Windows 中的错误。启用所有日志记录,并可能使用调试器和配置文件来查看时间花在了哪里。

    作为一种快速解决方法,您可以考虑使用不同的协议,例如 SSH 或 rsync 与 SSH。

    或者看看像XtreemFS 这样的远程文件系统(不过在你的情况下可能有点过分了)。

    【讨论】:

      【解决方案2】:

      使用缓冲时从/到大多数资源的流式传输速度更快。

      为此目的使用BufferedInputStreamBufferedOutputStream

      OutputStream os = new BufferedOutputStream(new FileOutputStream("/home/test/Access.mdb"));
      InputStream is = new BufferedInputStream(remoteFile.getInputStream());
      

      在关闭OutputStream 之前,总是flush(),当任何包装的流使用缓冲时,这一点至关重要。关闭而不刷新会导致数据丢失。

      os.flush();
      os.close();
      is.close();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-12
        • 2016-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        相关资源
        最近更新 更多