2012-05-03 102 views
1

在下面的代碼聲明:jcifs.smb.SmbException:訪問被拒絕。例外SMB目錄

SmbFileInputStream din==new SmbFileInputStream(src); 

我想創建一個SmbFileInputStream對象。

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) 

什麼不對這個代碼:如果SmbFile「src」中是一個文件,但如果「src」中是一個SMB目錄則拋出異常,這樣就可以正常工作?或我哪裏錯了?

你好請驗證碼:

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; 

回答

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 
} 
+0

那麼我該如何複製目錄到我的android手機的SD卡?因爲如果我在jcifs包中使用SmbFile類的copyTo()方法,我無法創建android的目標文件(即「/ mnt/sdcard /」)作爲smbFile對象。那麼如何實現目錄複製? – ram

+0

您需要首先使用'new SmbFile(file).mkdirs()'創建所有目錄,然後打開一個'SmbFileInputStream'來將文件寫入它。看到我編輯的代碼。 – wattostudios

+0

嘿,我需要在SD卡創建目錄?因爲在我使用FileOutputStream寫入SD卡之前,上面的異常被拋出.. – ram