java.io.FileNotFoundException:打开失败:上传csv时EACCES(权限被拒绝) [英] java.io.FileNotFoundException: open failed: EACCES (Permission denied) when uploading csv
问题描述
我正在将文件上传到我的 php 网络服务器,但是我的权限被拒绝.它适用于 mp3 和图像扩展.但是对于 .csv,当我点击上传时我会得到那个错误.我是新手,我不知道是 android 上的权限问题还是上传不支持上传 csv.感谢任何指导或帮助.谢谢!
I am uploading a file to my php webserver, but however I'm getting the permission denied. It works for mp3 and image extensions. However for .csv, i'll get that error when i hit upload. I'm new to this and I can't figure out if its the permission issue on android or the upload doesn't support uploading of csv. Appreciate any guidance or help. Thanks!
问题在于 FileInputStream fileInputStream = new FileInputStream(sourceFile);
部分,它会遇到错误并且不会过去.但如果是图片扩展名或音乐,则不会显示权限错误并成功上传.
The issue lies with the FileInputStream fileInputStream = new FileInputStream(sourceFile);
part where it will hit error and won't go past there. But if its an image extention or music, it will not show the permission error and successfully uploads.
Android Studio 代码
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 = "\r\n";
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";
}
}
我的 PHP 网络服务器脚本
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>
推荐答案
您报告的错误是由于所谓的范围存储"而产生的安卓 11
根据我所做的测试,这也会发生:
1) 在/storage/emulated/0/Download/Test
中创建一个文件夹(来自您的应用)2) 从应用程序中创建一个 test.csv 文件
3) 修改文件内容或用用户或服务器的 csv 文件覆盖它.通过 usb PC、NETWORK或使用X-plore、Es-explorer等的Android
The error you report is generated due to what is called "Scoped storage" of android 11
From the tests I have done this also occurs:
1) Create a folder (from your app) in /storage/emulated/0/Download/Test
2) Create inside a test.csv file from the app
3) Modify the content of the file or overwrite it with the user's or server's csv file. via usb PC, NETWORK or from Android using X-plore, Es-explorer, etc.
您刚刚失去了对该文件的访问权限!
You just lost access to that file!
基本上,您创建自己的目录,该目录以前甚至不存在,如果手动粘贴这些文件或通过其他具有访问权限的应用程序粘贴,您会发现自己无法访问其中包含的文件......我认为这是一个异常,我希望我们这么说.
Basically you create your own directory, which did not even exist before and you find yourself not having access to the files contained in it if these are pasted manually or through another app that has access ... I think it is an anomaly, I hope so we say.
Android 11 上的解决方案是使用访问驻留在外部目录中的文件的新系统来完全检查数据的读取和写入,或者更好的是它们退避并保留(至少对于由同一开发人员创建的目录)和以前一样......希望在谷歌常识中
The solution on Android 11 is to completely review the reading and writing of data with the new system of accessing files residing in external directories or even better is that they back off and stay (at least for directories created by the same developer) as before...hopefully in google common sense
查阅文档(我还没有理解我读过的内容......我希望):Android 11 中的存储更新
Consult the documentation (I have not understood what I have read...I hope): Storage updates in Android 11
这篇关于java.io.FileNotFoundException:打开失败:上传csv时EACCES(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!