简体   繁体   English

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

[英]How to copy file from local to share with JCifs?

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 copyTo method: 但是我在copyTo方法上遇到异常:

Invalid operation for workgroups or servers

How should I copy file from local to share? 我应该如何从本地复制文件以共享?

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

It was some time ago I worked with jcifs. 不久前,我与jcifs合作。 Could you try newFile.createNewFile(); 您可以尝试newFile.createNewFile(); and then use the copyTo . 然后使用copyTo If that doesn't work, than try newFile.getOutputStream() and write the data to this stream instead of using copyTo . 如果这不起作用,则尝试使用newFile.getOutputStream()并将数据写入此流,而不是使用copyTo

I´ma 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();

An example using Java 1.7's Files class: 使用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);
}
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... 这段代码很好用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM