使用JCIFS写入Windows共享的权限错误

时间:2015-02-04 12:54:39

标签: android permissions smb jcifs

我正在编写一个简单的应用程序将文件复制到本地Windows网络上的共享,但在执行代码时会收到以下错误。我正在使用JCIFS库和SmbFileOutputStream。

java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.ExceptionInInitializerError
        at jcifs.UniAddress.<clinit>(UniAddress.java:62)
        at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:864)
        at jcifs.smb.SmbFile.connect(SmbFile.java:951)
        at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
        at jcifs.smb.SmbFile.open0(SmbFile.java:972)
        at jcifs.smb.SmbFile.open(SmbFile.java:1006)
        at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:142)
        at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:97)
        at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:67)
        at com.ioxis.ian.pcupload.MainActivity$CopySMBFile.doInBackground(MainActivity.java:119)
        at com.ioxis.ian.pcupload.MainActivity$CopySMBFile.doInBackground(MainActivity.java:101)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
        at java.net.InetAddress.getLocalHost(InetAddress.java:365)
        at jcifs.netbios.NbtAddress.<clinit>(NbtAddress.java:187)
            at jcifs.UniAddress.<clinit>(UniAddress.java:62)
            at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:864)
            at jcifs.smb.SmbFile.connect(SmbFile.java:951)
            at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
            at jcifs.smb.SmbFile.open0(SmbFile.java:972)
            at jcifs.smb.SmbFile.open(SmbFile.java:1006)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:142)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:97)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:67)
            at com.ioxis.ian.pcupload.MainActivity$CopySMBFile.doInBackground(MainActivity.java:119)
            at com.ioxis.ian.pcupload.MainActivity$CopySMBFile.doInBackground(MainActivity.java:101)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
 Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
        at libcore.io.Posix.getaddrinfo(Native Method)
        at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
            at java.net.InetAddress.getLocalHost(InetAddress.java:365)
            at jcifs.netbios.NbtAddress.<clinit>(NbtAddress.java:187)
            at jcifs.UniAddress.<clinit>(UniAddress.java:62)
            at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:864)
            at jcifs.smb.SmbFile.connect(SmbFile.java:951)
            at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
            at jcifs.smb.SmbFile.open0(SmbFile.java:972)
            at jcifs.smb.SmbFile.open(SmbFile.java:1006)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:142)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:97)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:67)
            at com.ioxis.ian.pcupload.MainActivity$CopySMBFile.doInBackground(MainActivity.java:119)
            at com.ioxis.ian.pcupload.MainActivity$CopySMBFile.doInBackground(MainActivity.java:101)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
 Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
            at libcore.io.Posix.getaddrinfo(Native Method)
            at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
            at java.net.InetAddress.getLocalHost(InetAddress.java:365)
            at jcifs.netbios.NbtAddress.<clinit>(NbtAddress.java:187)
            at jcifs.UniAddress.<clinit>(UniAddress.java:62)
            at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:864)
            at jcifs.smb.SmbFile.connect(SmbFile.java:951)
            at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
            at jcifs.smb.SmbFile.open0(SmbFile.java:972)
            at jcifs.smb.SmbFile.open(SmbFile.java:1006)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:142)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:97)
            at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:67)
            at com.ioxis.ian.pcupload.MainActivity$CopySMBFile.doInBackground(MainActivity.java:119)
            at com.ioxis.ian.pcupload.MainActivity$CopySMBFile.doInBackground(MainActivity.java:101)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

目标共享和文件夹具有为Everyone设置的完全控制权限(它位于Windows 2008服务器上)。我也尝试过Windows 7工作站上的共享文件夹,但是出现了同样的错误。 我的Android清单中的代码和权限如下。我已为应用启用了这些权限: ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE,INTERNET

class CopySMBFile extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected String doInBackground(String... f_url) {
        try {
            String fileContent = "This is a test file";
            try{
                String user = "myusername:mypassword";
                System.out.println("User: " + user);

                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
                String path = "smb://192.168.1.4/k/temp/fileName.txt";
                System.out.println("Path: " +path);

                SmbFile sFile = new SmbFile(path, auth);
                SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
                sfos.write(fileContent.getBytes());

                System.out.println("Finished attempt to write file");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Connected", e.getMessage());
        }

        return null;
    }

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
</application>

有关如何修复(或进一步诊断)此问题的任何想法?

1 个答案:

答案 0 :(得分:1)

将您的<uses-permission>元素移至<application>的同行,而不是<application>的子女。