文件传输通过samba协议android错误

时间:2015-10-01 21:20:51

标签: android jcifs

我想将文件从我的应用程序传输到无线samba设备。我写了以下代码但没有工作。设备有ip = 192.168.1.2:8181,用户名= Admin,密码= [空]。设备显示完整我试图从我的电脑浏览器访问它时的文件。

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.io.File;
import java.io.FileOutputStream;
import java.net.MalformedURLException;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;


public class Samba extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_samba);



        Thread t = new Thread(new Runnable() {
            public void run() {
        /*
         * Do something
         */

                try {
                    String yourPeerPassword = "";
                    String yourPeerName = "Admin";
                    String yourPeerIP = "192.168.1.2:8181";

                    String smfilepath = "smb://" + yourPeerIP + "//dummy.txt.txt";

                    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
                            null, yourPeerName, yourPeerPassword);

                    Log.e("Connected", "Yes");
                    SmbFile smbFile = new SmbFile(smfilepath, auth);
                    /** Printing Information about SMB file which belong to your Peer **/
                    String nameoffile = smbFile.getName();
                    String pathoffile = smbFile.getPath();
                    Log.e(nameoffile, pathoffile);
                    Log.e("Path", "Yes");

                    SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
                    smbfos.write("testing....and writing to a file".getBytes());
                    smbfos.close();
/*
                    SmbFile Dest=new SmbFile(smfilepath,auth);

                    Log.e("file", "dest");
                    downloadFileFromPeerToSdcard(Dest);
                    Log.e("after fun", "Yes");*/
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    Log.e("MalformURL", e.getMessage());
                } catch (SmbException e) {
                    e.printStackTrace();
                    Log.e("SMBException", e.getMessage());
                }catch (Exception e) {
                    e.printStackTrace();
                    Log.e("Not Connected", e.getMessage());
                }
            }
        });

        t.start();



    }
}

记录错误:

10-05 09:47:41.220    6332-7651/loginscreen.example.com.girviapp E/Connected﹕ Yes
10-05 09:47:41.220    6332-7651/loginscreen.example.com.girviapp E/dummy.txt.txt﹕ smb://192.168.1.2:8181//dummy.txt.txt
10-05 09:47:41.220    6332-7651/loginscreen.example.com.girviapp E/Path﹕ Yes
10-05 09:47:49.897    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ jcifs.smb.SmbException: Failed to connect: 0.0.0.0<00>/192.168.1.2
10-05 09:47:49.897    6332-7545/loginscreen.example.com.girviapp W/art﹕ Long monitor contention event with owner method=void jcifs.smb.SmbTree.treeConnect(jcifs.smb.ServerMessageBlock, jcifs.smb.ServerMessageBlock) from SmbTree.java:136 waiters=0 for 22.506s
10-05 09:47:49.897    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ jcifs.util.transport.TransportException: Connection timeout
10-05 09:47:49.897    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ at jcifs.util.transport.Transport.connect(Transport.java:174)
10-05 09:47:49.897    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ at jcifs.smb.SmbTransport.connect(SmbTransport.java:307)
10-05 09:47:49.897    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ at jcifs.smb.SmbTree.treeConnect(SmbTree.java:156)
10-05 09:47:49.898    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
10-05 09:47:49.898    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ at jcifs.smb.SmbFile.connect(SmbFile.java:957)
10-05 09:47:49.898    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
10-05 09:47:49.898    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ at jcifs.smb.SmbFile.open0(SmbFile.java:975)
10-05 09:47:49.898    6332-7490/loginscreen.example.com.girviapp W/System.err﹕ at jcifs.smb.SmbFile.open(SmbFile.java:1009)

1 个答案:

答案 0 :(得分:0)

您应该安装日志收集器应用程序以确定是否生成了任何错误。

This可能会有所帮助。

之后,您可以在错误日志中找到可能的原因。 如果你无法写入文件,至少应该抛出一个IOException。

此外,双重检查您没有共享文件夹名称(也就是说,您确实要写入SMB设备上未命名的根文件夹)可能会有所帮助,如果是这样,请删除之后的两个斜杠之一来自SMB网址的端口(应在其中放置共享名称)可能会有所帮助。