以下代码转到“开始上传文件”和“尝试从服务器删除文件...”然后如果调用“onFileCreate”或“onFileDelete”,则不会上传或删除文件,也不会抛出任何错误,以提供任何关于它被阻止的想法。我已经等了5分钟才能做点什么,没什么。有没有人知道我可以投入哪些内容,让我更多地了解最新情况并最终完成上传和删除?
public void onFileCreate(File file) {
try {
FTPClient ftpClient = new FTPClient();
System.out.println("File Created! - Do Work!" + file);
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream inputStream = new FileInputStream(file);
String filename = file.getPath();
System.out.println("Start uploading file");
boolean done = ftpClient.storeFile(filename, inputStream);
inputStream.close();
if (done) {
System.out.println("The file was uploaded successfully: " + file);
}
ftpClient.isConnected();
ftpClient.logout();
ftpClient.disconnect();
} catch(Exception e){
e.printStackTrace(System.err);
}
}
@Override
public void onFileDelete(File file) {
try {
System.out.println("File removed: Do Work!");
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String filename = file.getPath();
System.out.println("Trying to delete file from server...");
boolean done = ftpClient.deleteFile(filename);
if(done){
ftpClient.isConnected();
ftpClient.logout();
ftpClient.disconnect();
System.out.println("File deleted from server: " + file);
}
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
以下是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
我是个白痴....我正在使用storeFile命令的完整文件路径(而不是文件名)。
而不是:
String filename = file.getPath();
必须是:
String filename = file.getName();
第一个字符串命令是远程位置,因此显然本地文件路径不起作用。