-1

我在 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)
4

1 回答 1

1

错误消息指出:

java.io.FileNotFoundException:/mnt/sdcard:打开失败:EISDIR(是一个目录

在您的代码中,您正在执行以下操作:

new FileOutputStream(
  Environment.getExternalStorageDirectory() // <- This points to /mnt/sdcard
);

现在,正如FileOutputStream-documentation 所述

抛出:
FileNotFoundException -如果文件存在但是是目录而不是常规文件,不存在但无法创建,或者由于任何其他原因无法打开

所以,使用类似这样的参数:

new FileOutputStream(
  Environment.getExternalStorageDirectory()+"/"+filename
);

这将创建一个名称存储在filenameSD 卡中的文件并写入其中。

于 2012-12-15T15:49:11.807 回答