Java 如何从本地复制文件以与 JCifs 共享?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22934918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:21:50  来源:igfitidea点击:

How to copy file from local to share with JCifs?

javasambajcifs

提问by victorio

I can copy file from share to local. But I want to switch, and copy a file from local to share.

我可以将文件从共享复制到本地。但是我想切换,从本地复制一个文件来共享。

I am trying this code:
SmbFile oldFile = new SmbFile("c:/tmp/test.xml");
SmbFile newFile = new SmbFile("smb://someone_pc/tmp/test.xml", new NtlmPasswordAuthentication("", username, password));
oldFile.copyTo(newFile);

But I am getting an exception on copyTomethod:

但是我在copyTo方法上遇到了异常:

Invalid operation for workgroups or servers

How should I copy file from local to share?

我应该如何从本地复制文件以共享?

回答by TomasZ.

It was some time ago I worked with jcifs. Could you try newFile.createNewFile();and then use the copyTo. If that doesn't work, than try newFile.getOutputStream()and write the data to this stream instead of using copyTo.

前段时间我与 jcifs 一起工作。你可以尝试newFile.createNewFile();然后使用copyTo. 如果这不起作用,请尝试newFile.getOutputStream()将数据写入此流而不是使用copyTo.

回答by Javier Vazquez

I′m a bit late to the party but this could be useful for other persons coming to this question.

我参加聚会有点晚了,但这对其他提出这个问题的人可能有用。

I upload files from local to share using streams and it′s working without any problem. My code is:

我从本地上传文件以使用流共享,它的工作没有任何问题。我的代码是:

SmbFile remoteFile =  new SmbFile(remotePath, auth);
SmbFileOutputStream out = new SmbFileOutputStream(remoteFile);
FileInputStream fis = new FileInputStream(localFile);
out.write(IOUtils.toByteArray(fis));
out.close();

回答by Radek

Complete solution for copying file from local to shared disk over SMB protocol using streams like Javi_Swift(writing in parts - solution for large files where it's not possible to load whole file into memory):

使用Javi_Swift 等流通过 SMB 协议将文件从本地复制到共享磁盘的完整解决方案(分部分写入 - 无法将整个文件加载到内存中的大文件的解决方案):

// local source file and target smb file
File fileSource = new File("C:/example.jpg");
SmbFile smbFileTarget = new SmbFile(smbFileContext, "example.jpg");
// input and output stream
FileInputStream fis = new FileInputStream(fileSource);
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFileTarget);
// writing data
try {
    // 16 kb
    final byte[] b  = new byte[16*1024];
    int read = 0;
    while ((read=fis.read(b, 0, b.length)) > 0) {
        smbfos.write(b, 0, read);
    }
}
finally {
    fis.close();
    smbfos.close();
}

回答by holmis83

An example using Java 1.7's Filesclass:

使用 Java 1.7 的Files类的示例:

Path source = Paths.get("c:/tmp/test.xml");
SmbFile newFile = new SmbFile("smb://someone_pc/tmp/test.xml", new NtlmPasswordAuthentication("", username, password));
try (OutputStream out = newFile.getOutputStream())
{
    Files.copy(source, out);
}

回答by balu

public void uploadToSmb(String destinationPath,File localFile){
    public final static byte[] BUFFER = new byte[10 * 8024];
        ByteArrayInputStream inputStream = null;
        SmbFileOutputStream sfos = null;
        try {
            String user = username + ":" + password;
            int lenghtOfFile = (int) localFile.length();
            byte[] data = FileUtils.readFileToByteArray(localFile);
            inputStream = new ByteArrayInputStream(data);
            String path = destinationPath + localFile.getName();
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
            remoteFile = new SmbFile(path, auth);
            sfos = new SmbFileOutputStream(remoteFile);
            long total = 0;
            while ((count = inputStream.read(BUFFER)) > 0) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                int percentage = (int) ((total / (float) lenghtOfFile) * 100);
                publishProgress(percentage);
                //  publishProgress((int) ((total * 100) / lenghtOfFile));
                // writing data to file
                    sfos.write(BUFFER,0,count);

            }
                sfos.flush();
                inputStream.close();
                sfos.close();

        } catch (Exception e) {
            e.printStackTrace();
        } 
      }  

this code is working Great...

这段代码很好用...