本文介绍了jcifs.smb.SmbException:访问被拒绝。例外SMB目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在低于code语句:

  SmbFileInputStream喧嚣==新SmbFileInputStream(SRC);

我想创建一个SmbFileInputStream对象。这将工作正常,如果SmbFile'src'中是一个文件,但如果'src'中是一个SMB目录则抛出异常:

  jcifs.smb.SmbException:访问被拒绝。
在jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:622)
在jcifs.smb.SmbTransport.send(SmbTransport.java:722)
在jcifs.smb.SmbSession.send(SmbSession.java:262)
在jcifs.smb.SmbTree.send(SmbTree.java:119)
在jcifs.smb.SmbFile.send(SmbFile.java:785)
在jcifs.smb.SmbFile.open0(SmbFile.java:1009)
在jcifs.smb.SmbFile.open(SmbFile.java:1026)
在jcifs.smb.SmbFileInputStream<&初始化GT;(SmbFileInputStream.java:73)
在jcifs.smb.SmbFileInputStream<&初始化GT;(SmbFileInputStream.java:65)
在testhelp.main(testhelp.java:25)

什么是错的这个code?或者我在哪里去了?

喜请检查该code:

 情况下下载2:/ *此code代码段用于从SMB网络文件/文件夹下载到Android SD卡。
当我运行此code的抛出一些异常。它有评论在以往必要的。在code的其余部分是自我
自明。所以,请通过code和请告诉为什么这个异常。
请尽量加入一个进度条,这将有助于用户谣谚一些工作是怎么回事。
我曾尝试包括一个进度条,但它不工作。我读过与此相关的一些材料,
但每件事情让使用线程。我并不擅长线程。那么,这可能包括一个酒吧陆侃,
不使用线程?如果可以的话,请帮我做。
而这code工作文件SMB文件,我不知道为什么在目录中的情况下,它投掷例外。
请看看为什么抛出异常..
所以,请看到修改code包含:
一)无例外
b)在进度条(更具体地说单杠)* // *抛出异常:
jcifs.smb.SmbException:访问被拒绝。
在jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:622)
在jcifs.smb.SmbTransport.send(SmbTransport.java:722)
在jcifs.smb.SmbSession.send(SmbSession.java:262)
在jcifs.smb.SmbTree.send(SmbTree.java:119)
在jcifs.smb.SmbFile.send(SmbFile.java:785)
在jcifs.smb.SmbFile.open0(SmbFile.java:1009)
在jcifs.smb.SmbFile.open(SmbFile.java:1026)
在jcifs.smb.SmbFileInputStream<&初始化GT;(SmbFileInputStream.java:73)
在jcifs.smb.SmbFileInputStream<&初始化GT;(SmbFileInputStream.java:65)
在testhelp.main(testhelp.java:25)* /
体statfs体statfs =新体statfs(Environment.getExternalStorageDirectory()getAbsolutePath());
//如果SD卡安装则只有这一操作发生:
如果(Environment.getExternalStorageState()。等于(Environment.MEDIA_MOUNTED))
{
    //object.getCount()获取列表视图对象的数量
    的for(int i = 0; I< object.getCount();我++)
    {
        //在列表视图中的每个对象,如果被选中:
        如果(object.getter(我)==真)
        {
            SmbFileInputStream嚣= NULL;
            FileOutputStream中DOUT = NULL;
            尝试
            {
                //我已经使用了一个哈希表,映射列表视图名SMB对象
                SmbFile SRC = map.get(object.getItem(ⅰ));
                长块大小= statFs.getBlockSize();
                长FREESIZE = statFs.getFreeBlocks()*块大小;
                长差异= FREESIZE-src.length();
                布尔能= FALSE;
                如果((DIFF<!0))
                {
                    可以= TRUE;
                }
                如果(src.isHidden()及!&安培;可)
                {
                    尝试
                    {
                        如果(src.isFile())
                        {
                            DIN =新SmbFileInputStream(SRC);
                            DOUT =新的FileOutputStream(Environment.getExternalStorageDirectory()+/+ src.getName());
                        }
                        其他
                        {
                            DIN =新SmbFileInputStream(SRC);
                            文件OUTPUTFILE =新的文件(Environment.getExternalStorageDirectory()+/+ src.getName()); // 添加
                            outputFile.mkdirs(); // 添加
                            DOUT =新的FileOutputStream(OUTPUTFILE); // CHANGED
                        }
                        INT℃;
                        而((C = din.read())!= - 1)
                        {
                            dout.write(C);
                        }
                    }
                    最后
                    {
                        如果(DIN!= NULL)
                        {
                            din.close();
                        }
                        如果(DOUT!= NULL)
                        {
                            dout.close();
                        }
                    }
                }
                其他
                {
                    Toast.makeText(此,src.getName()+不能被下载,Toast.LENGTH_LONG).show();
                }
            }
            赶上(IOException异常E)
            {
                Toast.makeText(这一点,下载失败 - IO异常\\ n+ E,Toast.LENGTH_LONG).show();
            }
        }
    }
}
其他
{
    Toast.makeText(这一点,下载失败 - 无SD卡找到,Toast.LENGTH_LONG).show();
}
返回true;


解决方案

您不能创建一个 SmbFileInputStream 的目录,因为你不能读/写直接到目录对象。目录没有任何内容,至少不是一个文件的内容是一样的。

如果你想读一个目录的内容,你应该使用 SmbFile 来代替(例如,使用 listFiles( )方法)。在 SmbFileInputStream 对象只能从文件读取信息。

要写入文件到一个目录下,这样做...

 文件fil​​e =新的文件(到/ mnt / SD卡/ FILENAME.TXT);
file.mkdirs(); //这将创建所有丢失的目录
FileOutputStream中OS =新的FileOutputStream(文件);
//现在写入文件数据
os.write(...);

在您的code,更改以下几行...

 尝试
   {
    DIN =新SmbFileInputStream(SRC);
    DOUT =新的FileOutputStream(Environment.getExternalStorageDirectory()+/+ src.getName());
    INT℃;
    而((C = din.read())!= - 1)

要这个......

 尝试
   {
    DIN =新SmbFileInputStream(SRC);    文件OUTPUTFILE =新的文件(Environment.getExternalStorageDirectory()+/+ src.getName()); // 添加
    outputFile.mkdirs(); // 添加    DOUT =新的FileOutputStream(OUTPUTFILE); // CHANGED
    INT℃;
    而((C = din.read())!= - 1)

此外更改以下...

 如果(src.isFile()){
    DIN =新SmbFileInputStream(SRC);
    // DOUT =新的FileOutputStream(Environment.getExternalStorageDirectory()+/+ src.getName()); //删除此行
    文件OUTPUTFILE =新的文件(Environment.getExternalStorageDirectory()+/+ src.getName()); // 添加
    outputFile.mkdirs(); // 添加
    DOUT =新的FileOutputStream(OUTPUTFILE); // 添加
}
其他{
    // DIN =新SmbFileInputStream(SRC); //删除此行
    文件OUTPUTFILE =新的文件(Environment.getExternalStorageDirectory()+/+ src.getName());
    outputFile.mkdirs();
    // DOUT =新的FileOutputStream(OUTPUTFILE); //删除此行
}

In the below code statement:

SmbFileInputStream din==new SmbFileInputStream(src);

i am trying to create a SmbFileInputStream object. This will works fine if SmbFile 'src' is a file, but if 'src' is a smb Directory then it throws exception:

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)

What is wrong with this code? or where am i going wrong?

Hi please check this code:

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;
解决方案

You can't create an SmbFileInputStream for a directory, because you can't read/write directly to the directory object. A directory doesn't have any content, at least not in the same way that a file has content.

If you're trying to read the contents of a directory, you should probably be using SmbFile instead (for example, use the listFiles() method). The SmbFileInputStream object is only for reading information from a file.

To write a file to a directory, do this...

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(...);

In your code, change the following few lines...

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

To this...

   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)

Also change the following...

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
}

这篇关于jcifs.smb.SmbException:访问被拒绝。例外SMB目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:37