我正在尝试通过sftp将测试文件上传到树莓派。我已经使用winscp将文件从主计算机成功传输到pi,但是在android上似乎无法正常工作。
我的应用程序是用kotlin编写的,但是我只能用kotlin找到关于sftp的很少信息,所以sftp类是用java编写的,但是是从MainActivity.kt调用的:
SftpClass.java
package com.mydomain.myapp;
import com.jcraft.jsch.*;
public class SftpClass {
public static void uploadFile(String filepath) throws JSchException, SftpException {
JSch jsch = new JSch();
Session session = jsch.getSession("pi","ipaddr");
session.setPassword("password");
session.connect();
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
sftpChannel.put(filepath, "/home/pi/Desktop");
}
}
MainActivity.kt的一部分:
private fun sftptransfer() {
var file ="/storage/emulated/0/Download/Testfile.txt"
toast("Upload starting...") //toast() just sends a long toast notification
SftpClass.uploadFile(file)
toast("Complete!")
}
但是应用程序一启动就会崩溃。这是LOGCAT:
2019-07-19 14:15:49.576 11583-11583/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mydomain.myapp, PID: 11583
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myapp/com.mydomain.myapp.MainActivity}: com.jcraft.jsch.JSchException: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:7009)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: com.jcraft.jsch.JSchException: android.os.NetworkOnMainThreadException
at com.jcraft.jsch.Util.createSocket(Util.java:349)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.mydomain.myapp.SftpClass.uploadFile(SftpClass.java:19)
at com.mydomain.myapp.MainActivity.sftptransfer(MainActivity.kt:32)
at com.mydomain.myapp.MainActivity.onCreate(MainActivity.kt:26)
at android.app.Activity.performCreate(Activity.java:7163)
at android.app.Activity.performCreate(Activity.java:7154)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3087)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:7009)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:389)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:621)
at java.net.Socket.connect(Socket.java:570)
at java.net.Socket.<init>(Socket.java:450)
at java.net.Socket.<init>(Socket.java:218)
at com.jcraft.jsch.Util.createSocket(Util.java:343)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.mydomain.myapp.SftpClass.uploadFile(SftpClass.java:19)
at com.mydomain.myapp.MainActivity.sftptransfer(MainActivity.kt:32)
at com.mydomain.myapp.MainActivity.onCreate(MainActivity.kt:26)
at android.app.Activity.performCreate(Activity.java:7163)
at android.app.Activity.performCreate(Activity.java:7154)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3087)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:7009)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-19 14:15:49.578 11583-11583/? W/OPDiagnose: getService:OPDiagnoseService NULL
2019-07-19 14:15:49.584 11583-11705/? D/OSTracker: OS Event: crash
2019-07-19 14:15:49.600 11583-11705/? D/AbstractTracker: Event success
我对kotlin和Java还是很陌生,因此,您所能提供的任何帮助将不胜感激。
编辑:
这是我根据评论尝试的代码:
import android.os.AsyncTask;
import com.jcraft.jsch.*;
import java.io.File;
public class SftpClass extends AsyncTask<String, Void, Boolean> {
private Exception exception;
protected Boolean doInBackground(String... path) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession("pi", "ip");
session.setPassword("password");
session.connect();
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
sftpChannel.put("/storage/emulated/0/Download/Testfile.txt", "/home/pi/Desktop/");
return true;
}
catch (Exception e) {
this.exception = e;
return false;
}
}
protected void onPostExecute(boolean status) {
}
}