我在 android apache ftp 客户端下载一个文件。虽然我没有收到错误但在 Logcat 中收到警告
这是代码
public void downloadFile(String filename) {
BufferedOutputStream desFileStream = null;
try {
desFileStream = new BufferedOutputStream(new FileOutputStream(
Environment.getExternalStorageDirectory()),
8 * 1024);
mFtp.retrieveFile(filename, desFileStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我的堆栈跟踪是
12-15 20:17:30.170: W/System.err(19182): java.io.FileNotFoundException: /mnt/sdcard: open failed: EISDIR (Is a directory)
12-15 20:17:30.180: W/System.err(19182): at libcore.io.IoBridge.open(IoBridge.java:406)
12-15 20:17:30.180: W/System.err(19182): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
12-15 20:17:30.180: W/System.err(19182): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
12-15 20:17:30.215: W/System.err(19182): at com.murtaza.msftp.myftpTask.downloadFile(myftpTask.java:82)
12-15 20:17:30.220: W/System.err(19182): at com.murtaza.msftp.MainActivity$1.onItemClick(MainActivity.java:74)
12-15 20:17:30.281: W/System.err(19182): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
12-15 20:17:30.281: W/System.err(19182): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
12-15 20:17:30.307: W/System.err(19182): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
12-15 20:17:30.320: W/System.err(19182): at android.widget.AbsListView$1.run(AbsListView.java:3168)
12-15 20:17:30.330: W/System.err(19182): at android.os.Handler.handleCallback(Handler.java:605)
12-15 20:17:30.330: W/System.err(19182): at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 20:17:30.330: W/System.err(19182): at android.os.Looper.loop(Looper.java:137)
12-15 20:17:30.330: W/System.err(19182): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-15 20:17:30.330: W/System.err(19182): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 20:17:30.340: W/System.err(19182): at java.lang.reflect.Method.invoke(Method.java:511)
12-15 20:17:30.340: W/System.err(19182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 20:17:30.340: W/System.err(19182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 20:17:30.340: W/System.err(19182): at dalvik.system.NativeStart.main(Native Method)
12-15 20:17:30.359: W/System.err(19182): Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
12-15 20:17:30.380: W/System.err(19182): at libcore.io.Posix.open(Native Method)
12-15 20:17:30.380: W/System.err(19182): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:98)
12-15 20:17:30.380: W/System.err(19182): at libcore.io.IoBridge.open(IoBridge.java:390)
12-15 20:17:30.380: W/System.err(19182): ... 17 more
实施后
Environment.getExternalStorageDirectory() + "/"+ filename
给出空指针异常。StackTrace 是
12-15 20:47:26.350: E/AndroidRuntime(26296): FATAL EXCEPTION: main
12-15 20:47:26.350: E/AndroidRuntime(26296): java.lang.NullPointerException
12-15 20:47:26.350: E/AndroidRuntime(26296): at org.apache.commons.net.SocketClient.getRemoteAddress(SocketClient.java:651)
12-15 20:47:26.350: E/AndroidRuntime(26296): at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:732)
12-15 20:47:26.350: E/AndroidRuntime(26296): at org.apache.commons.net.ftp.FTPClient._retrieveFile(FTPClient.java:1778)
12-15 20:47:26.350: E/AndroidRuntime(26296): at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1769)
12-15 20:47:26.350: E/AndroidRuntime(26296): at com.murtaza.msftp.myftpTask.downloadFile(myftpTask.java:85)
12-15 20:47:26.350: E/AndroidRuntime(26296): at com.murtaza.msftp.MainActivity$1.onItemClick(MainActivity.java:74)
12-15 20:47:26.350: E/AndroidRuntime(26296): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
12-15 20:47:26.350: E/AndroidRuntime(26296): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
12-15 20:47:26.350: E/AndroidRuntime(26296): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
12-15 20:47:26.350: E/AndroidRuntime(26296): at android.widget.AbsListView$1.run(AbsListView.java:3168)
12-15 20:47:26.350: E/AndroidRuntime(26296): at android.os.Handler.handleCallback(Handler.java:605)
12-15 20:47:26.350: E/AndroidRuntime(26296): at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 20:47:26.350: E/AndroidRuntime(26296): at android.os.Looper.loop(Looper.java:137)
12-15 20:47:26.350: E/AndroidRuntime(26296): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-15 20:47:26.350: E/AndroidRuntime(26296): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 20:47:26.350: E/AndroidRuntime(26296): at java.lang.reflect.Method.invoke(Method.java:511)
12-15 20:47:26.350: E/AndroidRuntime(26296): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 20:47:26.350: E/AndroidRuntime(26296): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 20:47:26.350: E/AndroidRuntime(26296): at dalvik.system.NativeStart.main(Native Method)