我必须更新使用JCIFS for Windows Server 2012的旧文件共享软件。出于安全考虑,我们必须禁用SMB 1.0 / CIFS兼容性。有没有人知道可以在Java中使用的变通方法或修复或JCIFS的替代方法?
答案 0 :(得分:3)
我刚刚发布了一个类似问题的答案:
Accessing SMB2.1 or SMB3 share from java?
< ...> 我刚刚在Android上使用了hierynomus/smbj v0.2.0并添加了SMB2支持。 < ...> 需要Bouncycastle / Spongycastle < ...>
确保在openFile
上调用DiskShare
时设置正确的标记(通过查看源代码偏离课程):
// required imports
import com.hierynomus.msdtyp.AccessMask;
import com.hierynomus.msfscc.FileAttributes;
import com.hierynomus.mssmb2.SMB2CreateDisposition;
import com.hierynomus.mssmb2.SMB2CreateOptions;
import com.hierynomus.mssmb2.SMB2ShareAccess;
import com.hierynomus.smbj.SMBClient;
import com.hierynomus.smbj.SmbConfig;
import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.common.SMBApiException;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.DiskShare;
import com.hierynomus.smbj.share.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashSet;
...
// connection params
String sambaDomain = null; // can be null
String sambaUsername = "iamuploader";
String sambaPass = "mysecret";
String sambaIP = "192.168.1.236";
String sambaSharedPath = "sharedfolder";
...
// upload method
// usage: upload("file/whithin/folder.txt", fileBytes);
public void upload(String filename, byte[] bytes) throws IOException {
SmbConfig cfg = SmbConfig.builder().build();
SMBClient client = new SMBClient(cfg);
Connection connection = client.connect(sambaIP);
Session session = connection.authenticate(new AuthenticationContext(sambaUsername, sambaPass.toCharArray(), sambaDomain));
DiskShare share = (DiskShare) session.connectShare(sambaSharedPath);
// this is com.hierynomus.smbj.share.File !
File f = null;
int idx = filename.lastIndexOf("/");
// if file is in folder(s), create them first
if(idx > -1) {
String folder = filename.substring(0, idx);
try {
if(!share.folderExists(folder)) share.mkdir(folder);
} catch (SMBApiException ex) {
throw new IOException(ex);
}
}
// I am creating file with flag FILE_CREATE, which will throw if file exists already
if(!share.fileExists(filename)){
f = share.openFile(filename,
new HashSet<>(Arrays.asList(AccessMask.GENERIC_ALL)),
new HashSet<>(Arrays.asList(FileAttributes.FILE_ATTRIBUTE_NORMAL)),
SMB2ShareAccess.ALL,
SMB2CreateDisposition.FILE_CREATE,
new HashSet<>(Arrays.asList(SMB2CreateOptions.FILE_DIRECTORY_FILE))
);
}
if(f == null) return null;
OutputStream os = f.getOutputStream();
os.write(bytes);
os.close();
}
答案 1 :(得分:0)
Visuality Systems正在开发Java中的商业smb堆栈,并向SMB1提供后向支持。测试版很快就会推出。 VS还通过JNI在现有的NQE堆栈上提供完整的SMB。