java.io.FileNotFoundException:/storage/emulated/0/Download/smiley.csv:打开失败:EACCES(权限被拒绝) [英] java.io.FileNotFoundException: /storage/emulated/0/Download/smiley.csv: open failed: EACCES (Permission denied)

查看:150
本文介绍了java.io.FileNotFoundException:/storage/emulated/0/Download/smiley.csv:打开失败:EACCES(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文件上传到我的 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!

我的上传代码:

private class UploadFileAsync extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        try {
            String sourceFileUri = "/storage/emulated/0/Download/smiley.csv";
            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(sourceFileUri);

            Log.d("myTag", ""+sourceFile.isFile());
            if (sourceFile.isFile()) {

                try {
                    String upLoadServerUri = "https://www.mywebsite.tk/upload.php?";

                    // open a URL connection to the Servlet
                    FileInputStream fileInputStream = new FileInputStream(
                            sourceFile);
                    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", sourceFileUri);

                    dos = new DataOutputStream(conn.getOutputStream());

                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
                            + sourceFileUri + "\"" + 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 服务器代码

<?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.";
      }

?>

推荐答案

如果您想从 android 获取文件,请使用带有路径和文件的 File 构造函数.例如新文件(路径,文件);

If you wanna get a file from android, use File constructor with path and file. e.g. new File(path, file);

我的项目:检查 File#getFile(Context context, String fileName)

这篇关于java.io.FileNotFoundException:/storage/emulated/0/Download/smiley.csv:打开失败:EACCES(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆