java.io.FileNotFoundException: open failed: EACCES (Permissi



我正在上传一个文件到我的php web服务器,但是我得到了拒绝的权限。它适用于mp3和图像扩展。然而,对于。csv,我会得到这个错误,当我点击上传。我是新手,我不知道这是android的权限问题还是上传不支持上传csv。感谢任何指导和帮助。谢谢!

问题在于FileInputStream fileInputStream = new FileInputStream(sourceFile);部分,它将击中错误并且不会越过那里。但如果是图像扩展或音乐,则不会显示权限错误并成功上传。

Android Studio Code
private class UploadFileAsync extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
String path = "/storage/emulated/0/Download";
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(path, "smiley.csv");
Log.d("myTag", ""+sourceFile.isFile());
if (sourceFile.isFile()) {
try {
String upLoadServerUri = "https://www.mywebsite.tk/upload.php?";
// open a URL connection to the Servlet
Log.d("myTag", "is it here; juz before");
FileInputStream fileInputStream = new FileInputStream(sourceFile);
Log.d("myTag", "is it here; juz after");
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE",
"multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("bill", "Hello.csv");
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="bill";filename=""
+ "Hello.csv" + """ + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math
.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0,
bufferSize);
}
// send multipart form data necesssary after file
// data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn
.getResponseMessage();
if (serverResponseCode == 200) {
// messageText.setText(msg);
//Toast.makeText(ctx, "File Upload Complete.",
//      Toast.LENGTH_SHORT).show();
// recursiveDelete(mDirectory1);
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception e) {
// dialog.dismiss();
e.printStackTrace();
}
// dialog.dismiss();
} // End else block

} catch (Exception ex) {
// dialog.dismiss();
ex.printStackTrace();
}
Log.d("myTag", "I iz completed");
return "Executed";
}
}

My PHP Webserver Script

<?php

if (is_uploaded_file($_FILES['bill']['tmp_name'])) {
$uploads_dir = './';
$tmp_name = $_FILES['bill']['tmp_name'];
$pic_name = $_FILES['bill']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
}
else{
echo "File not uploaded successfully.";
}
?>

错误日志

2021-02-12 17:11:17.047 10481-10481/com.example.assignment_test1 D/myTag: This is my message
2021-02-12 17:11:17.054 10481-10531/com.example.assignment_test1 D/myTag: true
2021-02-12 17:11:17.062 10481-10531/com.example.assignment_test1 D/myTag: is it here; juz before
2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/smiley.csv: open failed: EACCES (Permission denied)
2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:492)
2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:160)
2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err:     at com.example.assignment_test1.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:152)
2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err:     at com.example.assignment_test1.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:128)
2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err:     at android.os.AsyncTask$3.call(AsyncTask.java:394)
2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2021-02-12 17:11:17.074 10481-10531/com.example.assignment_test1 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err:     at java.lang.Thread.run(Thread.java:923)
2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err:     at libcore.io.Linux.open(Native Method)
2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
2021-02-12 17:11:17.075 10481-10531/com.example.assignment_test1 W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 W/System.err:     at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542)
2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:478)
2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 W/System.err:  ... 9 more
2021-02-12 17:11:17.076 10481-10531/com.example.assignment_test1 D/myTag: I iz completed

这是我的目录的图像:Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment_test1">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Assignment_Test1"
android:requestLegacyExternalStorage="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

设备文件管理器指向文件

您报告的错误是由于所谓的"作用域存储"而产生的。android 11的

从测试我已经做了这也发生:
1)创建一个文件夹(从你的应用程序)在/storage/emululated/0/Download/Test
2)创建一个Test .csv文件从应用程序
3)修改文件的内容或覆盖它与用户或服务器的csv文件。通过
usb PC, NETWORK或Android使用X-plore, Es-explorer等

您刚刚失去了访问该文件的权限!

基本上你创建了你自己的目录,这个目录以前甚至不存在,你发现自己无法访问其中包含的文件,如果这些文件是手动粘贴的,或者通过另一个有访问权限的应用程序…我认为这是一种反常现象,我希望我们能这么说。

Android 11的解决方案是完全检查读取和写入数据的新系统访问文件驻留在外部目录,或者更好的是,他们退回并保留(至少对于由同一开发人员创建的目录)。希望谷歌常识

查阅文档(我没有理解我所读的…我希望):Android 11的存储更新

最新更新