使用 JCIFS 写入 Windows 共享的权限错误
Permission error using JCIFS to write to Windows share
我正在编写一个简单的应用程序来将文件复制到本地 Windows 网络上的共享,但在执行代码时出现以下错误。我正在使用 JCIFS 库和 SmbFileOutputStream。
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.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.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.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
目标共享和文件夹为所有人设置了完全控制权限(在 Windows 2008 服务器上)。我还在 Windows 7 工作站上尝试了一个共享文件夹,但得到了同样的错误。
我的 Android 清单中的代码和权限如下。我为该应用启用了这些权限:
ACCESS_NETWORK_STATE、ACCESS_WIFI_STATE、互联网
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>
关于如何解决(或进一步诊断)这个问题有什么想法吗?
将您的 <uses-permission>
元素移动到 <application>
的对等元素,而不是 <application>
的子元素。
我正在编写一个简单的应用程序来将文件复制到本地 Windows 网络上的共享,但在执行代码时出现以下错误。我正在使用 JCIFS 库和 SmbFileOutputStream。
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.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.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.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
目标共享和文件夹为所有人设置了完全控制权限(在 Windows 2008 服务器上)。我还在 Windows 7 工作站上尝试了一个共享文件夹,但得到了同样的错误。 我的 Android 清单中的代码和权限如下。我为该应用启用了这些权限: ACCESS_NETWORK_STATE、ACCESS_WIFI_STATE、互联网
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>
关于如何解决(或进一步诊断)这个问题有什么想法吗?
将您的 <uses-permission>
元素移动到 <application>
的对等元素,而不是 <application>
的子元素。