jcifs文件上传到另一台服务器,如何使用Java中的jcifs将文件从smb共享复制到本地驱动器?...

Man Called H..

7

经过多次试验和失败后,唯一可靠的方法就是老去学校并使用FileInputStream和FileOutputStream,如下所示:

`SmbFile[] files = getSMBListOfFiles(sb, logger, domain, userName, password, sourcePath, sourcePattern);

if (files == null)

return false;

output(sb, logger, " Source file count: " + files.length);

String destFilename;

FileOutputStream fileOutputStream;

InputStream fileInputStream;

byte[] buf;

int len;

for (SmbFile smbFile: files) {

destFilename = destinationPath + smbFile.getName();

output(sb, logger, " copying " + smbFile.getName());

try {

fileOutputStream = new FileOutputStream(destFilename);

fileInputStream = smbFile.getInputStream();

buf = new byte[16 * 1024 * 1024];

while ((len = fileInputStream.read(buf)) > 0) {

fileOutputStream.write(buf, 0, len);

}

fileInputStream.close();

fileOutputStream.close();

} catch (SmbException e) {

OutputHandler.output(sb, logger, "Exception during copyNetworkFilesToLocal stream to output, SMP issue: " + e.getMessage(), e);

e.printStackTrace();

return false;

} catch (FileNotFoundException e) {

OutputHandler.output(sb, logger, "Exception during copyNetworkFilesToLocal stream to output, file not found: " + e.getMessage(), e);

e.printStackTrace();

return false;

} catch (IOException e) {

OutputHandler.output(sb, logger, "Exception during copyNetworkFilesToLocal stream to output, IO problem: " + e.getMessage(), e);

e.printStackTrace();

return false;

}

}`

只需要注意一点,最好在finally块中关闭流. (3认同)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值