jcifs文件上传到另一台服务器,如何从本地复制文件以与JCifs共享?

5 个答案:

答案 0 :(得分:3)

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

}

答案 1 :(得分:2)

前一段时间我和jcifs合作过。

您可以尝试newFile.createNewFile();然后使用copyTo。

如果这不起作用,请尝试newFile.getOutputStream()并将数据写入此流而不是使用copyTo。

答案 2 :(得分:1)

使用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);

}

答案 3 :(得分:1)

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

我使用流将文件从本地上传到共享,并且它没有任何问题。我的代码是:

SmbFile remoteFile = new SmbFile(remotePath, auth);

SmbFileOutputStream out = new SmbFileOutputStream(remoteFile);

FileInputStream fis = new FileInputStream(localFile);

out.write(IOUtils.toByteArray(fis));

out.close();

答案 4 :(得分:0)

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

}

}

这段代码工作得很好......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值