【问题标题】:jcifs.smb.SmbException: Access is Denied. exception for smb directoriesjcifs.smb.SmbException:访问被拒绝。 smb 目录的例外
【发布时间】:2012-05-03 14:07:54
【问题描述】:

在下面的代码语句中:

SmbFileInputStream din==new SmbFileInputStream(src);

我正在尝试创建一个 SmbFileInputStream 对象。如果 SmbFile 'src' 是一个文件,这将正常工作,但如果 'src' 是一个 smb 目录,那么它会抛出异常:

jcifs.smb.SmbException: Access is Denied.
at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:622)
at jcifs.smb.SmbTransport.send(SmbTransport.java:722)
at jcifs.smb.SmbSession.send(SmbSession.java:262)
at jcifs.smb.SmbTree.send(SmbTree.java:119)
at jcifs.smb.SmbFile.send(SmbFile.java:785)
at jcifs.smb.SmbFile.open0(SmbFile.java:1009)
at jcifs.smb.SmbFile.open(SmbFile.java:1026)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
at testhelp.main(testhelp.java:25)

这段代码有什么问题?或者我哪里错了?

您好,请检查此代码:

case DOWNLOAD2:

/*This code snippet is used to download a file/folder from smb nETWORK to android sd card.
when I run this code its throwing some exception. It have commented where ever necessary. rest of the code is self
explanatory. So please go through the code and please tell why this exception is thrown.
IF POSSIBLE PLEASE ADD A PROGRESS BAR WHICH SHOULD HELP USER SAYING SOME WORK IS GOING ON.
I have tried including a progress bar, but its not working. I ve read some materials related to this,
but every thing makes use threads. I am not that good at threads. So is it possible to include a progess bar,
without using threads?If possible please help me to do it.
And this code is working file for smb files, I dont know why its throwing exception in case of directories.
Please see why this is throwing exception..
So please see that the modified code contains:
a)no exceptions
b)a progress bar(more specifically a horizontal bar)*/

/*exception thrown:
jcifs.smb.SmbException: Access is Denied.
at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:622)
at jcifs.smb.SmbTransport.send(SmbTransport.java:722)
at jcifs.smb.SmbSession.send(SmbSession.java:262)
at jcifs.smb.SmbTree.send(SmbTree.java:119)
at jcifs.smb.SmbFile.send(SmbFile.java:785)
at jcifs.smb.SmbFile.open0(SmbFile.java:1009)
at jcifs.smb.SmbFile.open(SmbFile.java:1026)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
at testhelp.main(testhelp.java:25)*/


StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); 
//if sd card is mounted then only this operation occur:
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
    //object.getCount() gets the number of objects in list view
    for(int i=0;i<object.getCount();i++)
    {
        //for each object in list view, if it is checked:
        if(object.getter(i)==true)
        {
            SmbFileInputStream din=null;
            FileOutputStream dout=null;
            try 
            {
                //I have used a hash table, which maps list view name with smb object
                SmbFile src=map.get(object.getItem(i));
                long blockSize = statFs.getBlockSize();
                long freeSize = statFs.getFreeBlocks()*blockSize;
                long diff=freeSize-src.length();
                boolean can=false;
                if(!(diff<0))
                {
                    can=true;
                }
                if(!src.isHidden() && can)
                {
                    try
                    {
                        if(src.isFile())
                        {
                            din=new SmbFileInputStream(src);
                            dout=new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+src.getName());
                        }
                        else
                        {
                            din=new SmbFileInputStream(src);
                            File outputFile = new File(Environment.getExternalStorageDirectory()+"/"+src.getName()); // ADDED
                            outputFile.mkdirs(); // ADDED
                            dout=new FileOutputStream(outputFile); // CHANGED
                        }
                        int c;
                        while((c=din.read())!=-1)
                        {
                            dout.write(c);
                        }
                    }
                    finally
                    {
                        if (din != null) 
                        {
                            din.close();
                        }
                        if (dout != null) 
                        {
                            dout.close();
                        }
                    }
                }
                else
                {
                    Toast.makeText(this,src.getName()+" cannot be downloaded",Toast.LENGTH_LONG).show();
                }
            } 
            catch(IOException e)
            {
                Toast.makeText(this,"DOWNLOAD FAILED--IO EXCEPTION\n"+e,Toast.LENGTH_LONG).show();
            }
        }
    }
}
else
{
    Toast.makeText(this,"DOWNLOAD FAILED--NO SD CARD FOUND",Toast.LENGTH_LONG).show();
}
return true;

【问题讨论】:

    标签: java android samba cifs jcifs


    【解决方案1】:

    您无法为目录创建SmbFileInputStream,因为您无法直接读取/写入目录对象。目录没有任何内容,至少不像文件有内容那样。

    如果您尝试读取目录的内容,您可能应该改用SmbFile(例如,使用listFiles() 方法)。 SmbFileInputStream 对象仅用于从文件中读取信息。

    要将文件写入目录,请执行以下操作...

    File file = new File("/mnt/sdcard/filename.txt");
    file.mkdirs(); // this creates all the directories that are missing
    FileOutputStream os = new FileOutputStream (file);
    // now write the file data
    os.write(...);
    

    在您的代码中,更改以下几行...

       try
       {
        din=new SmbFileInputStream(src);
        dout=new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+src.getName());
        int c;
        while((c=din.read())!=-1)
    

    到这里……

       try
       {
        din=new SmbFileInputStream(src);
    
        File outputFile = new File(Environment.getExternalStorageDirectory()+"/"+src.getName()); // ADDED
        outputFile.mkdirs(); // ADDED
    
        dout=new FileOutputStream(outputFile); // CHANGED
        int c;
        while((c=din.read())!=-1)
    

    同时更改以下内容...

    if(src.isFile()){
        din=new SmbFileInputStream(src);
        //dout=new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+src.getName());// REMOVE THIS LINE
        File outputFile = new File(Environment.getExternalStorageDirectory()+"/"+src.getName()); // ADDED
        outputFile.mkdirs();  // ADDED
        dout=new FileOutputStream(outputFile);  // ADDED
    }
    else {
        //din=new SmbFileInputStream(src); // REMOVE THIS LINE
        File outputFile = new File(Environment.getExternalStorageDirectory()+"/"+src.getName());
        outputFile.mkdirs(); 
        //dout=new FileOutputStream(outputFile); // REMOVE THIS LINE
    }
    

    【讨论】:

    • 那我如何将目录复制到我的android手机的sd卡​​?因为如果我在 jcifs 包中使用 SmbFile 类的 copyTo() 方法,我无法将 android 的目标文件(即“/mnt/sdcard/”)创建为 smbFile 对象。那么如何实现目录复制呢?
    • 您需要先使用new SmbFile(file).mkdirs() 创建所有目录,然后打开SmbFileInputStream 将文件写入其中。查看我编辑的代码。
    • 嘿,我需要在 sd 卡中创建目录吗?因为在我使用 FileOutputStream 写入 sd 卡之前,抛出了上述异常..
    • 是的,您需要先创建目录,然后才能将文件写入其中。如果您尝试使用像FileOutputStream 这样的普通文件类,您也可以在FileOutputStream 之前使用new File(filename).mkdirs() 来创建所有目标目录。我再次编辑了我的答案以使用它。
    • 您好 WATTO,请参阅上面的“您好,请检查此代码:”,请告诉我应该在哪里添加您的解释..或者如果可能,请编辑我的代码..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多