如何将音频文件从android上传到ec2服务器并使用python处理传入的音频文件?

时间:2019-10-07 12:06:16

标签: java android amazon-ec2

我一直试图将音频文件从android上传到ec2服务器,但无法获取所选文件的文件名。

Uri uri = data.getData();
String selectedPath = uri.toString();

我尝试了上面的代码,并得到了输出

content://com.android.providers.downloads.documents/document/17

但是当我尝试上传文件时,它无法将路径识别为文件。

  

这是我的 Upload.java 代码:

public class Upload {

Context context;
public Upload(Context context) {
    this.context = context;
}

String UPLOAD_URL= "<MY_SERVER_URL>";

private int serverResponseCode;

public String uploadVideo(String file) {

    String fileName = file;
    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(file);
    if (!sourceFile.isFile()) {
        Log.e("Huzza", "Source File Does not exist");
        return null;
    }

    try {
        FileInputStream fileInputStream = new FileInputStream(sourceFile);
        URL url = new URL(UPLOAD_URL);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        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("myFile", fileName);
        dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
        dos.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        Log.i("Huzza", "Initial .available : " + bytesAvailable);

        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        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);
        }

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        serverResponseCode = conn.getResponseCode();

        fileInputStream.close();
        dos.flush();
        dos.close();
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (serverResponseCode == 200) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                    .getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }
            rd.close();
        } catch (IOException ioex) {
        }
        return sb.toString();
    }else {
        return "Could not upload";
    }
}
}
  

这是调用者方法

private void uploadVideo() {
    class UploadVideo extends AsyncTask<Void, Void, String> {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.e("STRING", s);

        }

        @Override
        protected String doInBackground(Void... params) {
            Upload u = new Upload(getApplicationContext());
            String msg = u.uploadVideo(selectedPath);
            //Log.e("MSG", msg);
            return msg;
        }
    }
    UploadVideo uv = new UploadVideo();
    uv.execute();
}

我已经尝试了所有存在的解决方案,但事实证明,这些解决方案中的大多数都适用于Android KitKat及更低版本,而其他解决方案也不起作用。这些解决方案均不适用于Android Pie。

  

尝试过的解决方案:

     

documentation

     

How to get the Full file path from URI

     

Get real path from URI, Android KitKat new storage access framework

0 个答案:

没有答案