使用smbj将文件发送到共享文件夹,下面的代码给了我拒绝访问的问题。但是我已经在“网络位置”下挂载了同一个共享文件夹,我可以通过手动创建将文件写入同一个文件夹。我猜我用来写文件的选项是不正确的。代码:
public static void main(String args[]) {
SMBClient client = new SMBClient();
try (Connection connection = client.connect("10.48.36.248")) {
AuthenticationContext ac = new AuthenticationContext("test", "1234".toCharArray(), "");
Session session = connection.authenticate(ac);
// Connect to ShareCards
try (DiskShare share = (DiskShare) session.connectShare("CCS")) {
Set<FileAttributes> fileAttributes = new HashSet<>();
fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
Set<SMB2CreateOptions> createOptions = new HashSet<>();
createOptions.add(SMB2CreateOptions.FILE_NON_DIRECTORY_FILE);
createOptions.add(SMB2CreateOptions.FILE_WRITE_THROUGH);
File f = share.openFile(
"\\SWD\\Groups\\New Folder\\"
+ "Test.txt",
new HashSet(Arrays.asList(new AccessMask[] { AccessMask.FILE_ADD_FILE })), fileAttributes,
SMB2ShareAccess.ALL,
SMB2CreateDisposition.FILE_OVERWRITE_IF, createOptions);
OutputStream oStream = f.getOutputStream();
oStream.write("I am testing".getBytes());
oStream.flush();
oStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
client.close();
}我得到的例外是
Exception in thread "main" com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022): Create failed for \\10.48.36.248\CCS\SWD\Groups\New Folder\Test.txt更新: \10.48.36.248\CCS\SWD之后,"Groups“文件夹看起来像是另一个位置的快捷方式,也许这就是我被拒绝访问的原因?如何写入快捷文件夹?
发布于 2022-06-22 10:45:12
使用jcifs-ng实现,并且运行良好。
波姆
<dependency>
<groupId>eu.agno3.jcifs</groupId>
<artifactId>jcifs-ng</artifactId>
<version>2.1.7</version>
</dependency>代码:
import jcifs.CIFSContext;
import jcifs.CIFSException;
import jcifs.context.SingletonContext;
import jcifs.smb.NtlmPasswordAuthenticator;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
public class JCIFSTest {
public static void main(String[] args) {
try {
CIFSContext baseCxt = SingletonContext.getInstance();
CIFSContext auth = baseCxt.withCredentials(new NtlmPasswordAuthenticator("domain", "username", "password"));
SmbFile f = new SmbFile("smb://10.48.36.248/CCS/SWD/GROUPS/New Folder/test.txt", auth);
SmbFileOutputStream out = new SmbFileOutputStream(f,true);
out.write("Just test".getBytes());
f.close();
out.close();
} catch (CIFSException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}https://stackoverflow.com/questions/72614803
复制相似问题