java - 使用 SMBJ 将多部分文件写入远程服务器
问题描述
我是 SMBJ 的新手,我想知道如何使用位于https://github.com/hierynomus/smbj的 SMBJ 将文件写入 smb 共享。
我可以使用以下代码创建一个文本文件,但我想将一个多部分文件从表单写入远程服务器。
String fileName ="Labour Acting Letter.txt";
String fileContents = "Mary had a little lamb.";
SMBClient client = new SMBClient();
try (Connection connection = client.connect("server")) {
AuthenticationContext ac = new AuthenticationContext("username", "password".toCharArray(), "");
Session session = connection.authenticate(ac);
// Connect to Share
try (DiskShare share = (DiskShare) session.connectShare("fsmis_files")) {
for (FileIdBothDirectoryInformation f : share.list("FOLDER", "*.*")) {
System.out.println("File : " + f.getFileName());
}
//share.openFile(path, accessMask, attributes, shareAccesses, createDisposition, createOptions)
Set<FileAttributes> fileAttributes = new HashSet<>();
fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
Set<SMB2CreateOptions> createOptions = new HashSet<>();
createOptions.add(SMB2CreateOptions.FILE_RANDOM_ACCESS);
File f = share.openFile("FOLDER"+"\\"+fileName, new HashSet(Arrays.asList(new AccessMask[]{AccessMask.GENERIC_ALL})), fileAttributes, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OVERWRITE_IF, createOptions);
OutputStream oStream = f.getOutputStream();
oStream.write(fileContents.getBytes());
oStream.flush();
oStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
解决方案
推荐阅读
- javascript - 如何在我的网站上显示来自在线购物网站的产品?
- sql - Postgres 索引函数似乎根据调用时间创建不同的值
- javascript - 开玩笑的单元测试。模拟函数返回,监视函数调用
- google-apps-script - 如何将具有多行的 2 列单元格拆分为唯一的行,保留/复制周围的数据?
- git - 为什么我在 Git 上的一些提交是以我的用户名签名的,而其他的则是我的名字?
- r - 通过粘贴两个因子变量创建一个新变量
- javascript - 将 DirectionalLight 附加到玩家对象时的漂移阴影
- java - GridDB JAVA API 中的异常
- powershell - 从文件夹中删除用户权限
- javascript - 有没有人在不使用 SharePoint 框架的情况下将 React 或 TypeScript 或两者与 SharePoint 一起使用?