前言:
而今咱们对“javasmb”大约比较关怀,大家都需要了解一些“javasmb”的相关文章。那么小编在网上收集了一些关于“javasmb””的相关知识,希望兄弟们能喜欢,兄弟们快快来学习一下吧!最近打算研究一下Flutter,做一个可以浏览Samba的共享的文件浏览器。发现Flutter没有现成的插件可以用(libdsm不好用)。
于是就打算自己写一个插件。
Flutter的扩展分为2中,package和plugin。package是用纯dart实现,plugin可以调用平台原生代码。我打算封装一下smbj这个java实现的smb库。所以这里选择plugin。
参考这篇官方的文档:
首先,我们创建一个项目:
flutter create --org com.example --template=plugin --platforms=android -a java smbj
然后使用Android Studio打开这个项目。
然后在project视图中,选择android[smbj_android]右键单击,选择flutter-->Open Android module in Android Studio,在新的窗口打开android项目。
接着我们在新的Android Studio窗口中添加项目依赖。
File --> Project Structure,打开Project Structure对话框。选择Dependencies页。
点击小+号,Add Dependency。
Add Library Dependency,选择smbj:
添加依赖:'com.hierynomus:smbj:0.11.5' 'org.slf4j:slf4j-simple:1.7.36'
将SmbjPlugin.java文件替换成:
package biz.bbfox.smbj;import androidx.annotation.NonNull;import io.flutter.embedding.engine.plugins.FlutterPlugin;import io.flutter.plugin.common.BinaryMessenger;import io.flutter.plugin.common.MethodCall;import io.flutter.plugin.common.MethodChannel;import io.flutter.plugin.common.MethodChannel.MethodCallHandler;import io.flutter.plugin.common.MethodChannel.Result;import io.flutter.plugin.common.StandardMethodCodec;import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;import com.hierynomus.smbj.SMBClient;import com.hierynomus.smbj.SmbConfig;import com.hierynomus.smbj.auth.AuthenticationContext;import com.hierynomus.smbj.connection.Connection;import com.hierynomus.smbj.session.Session;import com.hierynomus.smbj.share.DiskShare;import java.io.IOException;/** * SmbjPlugin */public class SmbjPlugin implements FlutterPlugin, MethodCallHandler { /// The MethodChannel that will the communication between Flutter and native Android /// /// This local reference serves to register the plugin with the Flutter Engine and unregister it /// when the Flutter Engine is detached from the Activity private MethodChannel channel; @Override public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {// channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "smbj");// channel.setMethodCallHandler(this); BinaryMessenger messenger = flutterPluginBinding.getBinaryMessenger(); BinaryMessenger.TaskQueue taskQueue = messenger.makeBackgroundTaskQueue(); channel = new MethodChannel( messenger, "smbj", StandardMethodCodec.INSTANCE, taskQueue); channel.setMethodCallHandler(this); } @Override public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { if (call.method.equals("getPlatformVersion")) { result.success("Android " + android.os.Build.VERSION.RELEASE); } else if (call.method.equals("connectSmb")) { SmbConfig config = SmbConfig.builder().withMultiProtocolNegotiate(true).build(); SMBClient client = new SMBClient(config); String serverName = "your-ip"; String user = "your name"; String password = "your password"; try (Connection connection = client.connect(serverName)) { AuthenticationContext ac = new AuthenticationContext(user, password.toCharArray(), "DOMAIN"); Session session = connection.authenticate(ac); // Connect to Share try (DiskShare share = (DiskShare) session.connectShare("F")) { for (FileIdBothDirectoryInformation f : share.list("", "*")) { System.out.println("File : " + f.getFileName()); } } } catch (IOException e) { e.printStackTrace(); } } else { result.notImplemented(); } } @Override public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { channel.setMethodCallHandler(null); }}
这样platform一侧就准备好了。
现在来做dart这一侧:
打开文件lib/smbj.dart,添加以下代码:
static Future<void> connectSmb() async { await _channel.invokeListMethod('connectSmb'); }
然后在main.dart中调用:
@override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), ), body: Center( child: Column( children: <Widget>[ Text('Running on: $_platformVersion\n'), IconButton(onPressed: () {Smbj.connectSmb();}, icon: const Icon(Icons.add)), ], )), ), ); }
这样一个简单的示例就做好了。下面是运行之后的输出,可以看到相应的文件夹已经被list出来:
W/System.err(19765): [flutter-worker-3] INFO com.hierynomus.smbj.connection.PacketEncryptor - Initialized PacketEncryptor with Cipher << AES_128_GCM >>W/System.err(19765): [flutter-worker-3] INFO com.hierynomus.smbj.connection.Connection - Successfully connected to: your-ipW/System.err(19765): [flutter-worker-3] INFO com.hierynomus.smbj.connection.SMBSessionBuilder - Successfully authenticated share on 192.168.3.12, session is 149534051139605W/System.err(19765): [flutter-worker-3] INFO com.hierynomus.smbj.session.Session - Connecting to \\your-ip\F on session 149534051139605I/System.out(19765): File : $RECYCLE.BINI/System.out(19765): File : $UGMI/System.out(19765): File : 资料
这只是一个示例,等后面有时间了,把这个plugin完善好。
标签: #javasmb