Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用POST请求发送InputStream_Java_Python_Android Studio_Flask - Fatal编程技术网

Java 如何使用POST请求发送InputStream

Java 如何使用POST请求发送InputStream,java,python,android-studio,flask,Java,Python,Android Studio,Flask,我想使用POST请求将一个mp3文件从android studio发送到flask服务器。我已经从设备中读取了一个mp3文件,并存储在输入流对象中。flask服务器将请求标识为POST请求并创建mp3文件。但是当我播放mp3文件时,它不工作。所以连接部分没有任何问题。我想知道如何在POST请求中发送这个inputstream对象,并让它在服务器上成功播放mp3文件(Raspberry Pi) 获取mp3文件并存储在inputstream中的部分: if(!DocumentsContract.is

我想使用POST请求将一个mp3文件从android studio发送到flask服务器。我已经从设备中读取了一个mp3文件,并存储在输入流对象中。flask服务器将请求标识为POST请求并创建mp3文件。但是当我播放mp3文件时,它不工作。所以连接部分没有任何问题。我想知道如何在POST请求中发送这个inputstream对象,并让它在服务器上成功播放mp3文件(Raspberry Pi)

获取mp3文件并存储在inputstream中的部分:

if(!DocumentsContract.isDocumentUri(this, data.getData()))
    throw new RuntimeException("Not a documentsContract document");

try {
    InputStream is = getContentResolver().openInputStream(data.getData());

    new Main4Activity.httpAsyncTask417().execute();
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}
    //if(!DocumentsContract.isDocumentUri(this, data.getData()))
                    //    throw new RuntimeException("Not a documentsContract document");

                    try {
                        is = getContentResolver().openInputStream(data.getData());
                        uri=data.getData();
                        file_name=getFileName(uri);
                        new Main4Activity.httpAsyncTask417().execute();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
发送POST请求的部分:

public class httpAsyncTask417 extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... strings) {

        try {
            String url="http://172.17.57.132/post_songs";
            URL obj=new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            //add reuqest header
            con.setRequestMethod("POST");

            String urlParameters = "content=";

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            Log.v("HTTPDelete_Check3", "Get returned: " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result

        }catch(java.io.IOException ex) {

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void v) {

    }
}
    try {
            String url="http://172.17.59.97/post_songs?title="+file_name;
            URL obj=new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            //add reuqest header
            con.setRequestMethod("POST");

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            /*BufferedReader r = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = r.readLine()) != null) {
                wr.writeBytes(line+'\n');
            }*/
            //wr.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + uri.toString() +"\"" + "\r\n");

            byte []buffer = new byte[4096];
            int read = 0;

            while ( (read = is.read(buffer)) != -1 ) {
                wr.write(buffer, 0, read);
            }
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            Log.v("HTTPDelete_Check3", "Get returned: " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result

        }catch(java.io.IOException ex) {

        }
公共类httpAsyncTask417扩展异步任务{
@凌驾
受保护的Void doInBackground(字符串…字符串){
试一试{
字符串url=”http://172.17.57.132/post_songs";
URL obj=新URL(URL);
HttpURLConnection con=(HttpURLConnection)obj.openConnection();
//添加reuqest标题
con.setRequestMethod(“POST”);
字符串urlParameters=“content=”;
//发送邮寄请求
con.设置输出(真);
DataOutputStream wr=新的DataOutputStream(con.getOutputStream());
writeBytes(URL参数);
wr.flush();
wr.close();
int responseCode=con.getResponseCode();
Log.v(“HTTPDelete_Check3”,“Get returned:+responseCode”);
BufferedReader in=新的BufferedReader(
新的InputStreamReader(con.getInputStream());
字符串输入线;
StringBuffer响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
in.close();
//打印结果
}捕获(java.io.IOException-ex){
}
返回null;
}
@凌驾
受保护的void onPostExecute(void v){
}
}

我解决了这个问题。下面是解决方案

获取mp3文件并存储在inputstream中的部分:

if(!DocumentsContract.isDocumentUri(this, data.getData()))
    throw new RuntimeException("Not a documentsContract document");

try {
    InputStream is = getContentResolver().openInputStream(data.getData());

    new Main4Activity.httpAsyncTask417().execute();
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}
    //if(!DocumentsContract.isDocumentUri(this, data.getData()))
                    //    throw new RuntimeException("Not a documentsContract document");

                    try {
                        is = getContentResolver().openInputStream(data.getData());
                        uri=data.getData();
                        file_name=getFileName(uri);
                        new Main4Activity.httpAsyncTask417().execute();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
发送POST请求的部分:

public class httpAsyncTask417 extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... strings) {

        try {
            String url="http://172.17.57.132/post_songs";
            URL obj=new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            //add reuqest header
            con.setRequestMethod("POST");

            String urlParameters = "content=";

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            Log.v("HTTPDelete_Check3", "Get returned: " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result

        }catch(java.io.IOException ex) {

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void v) {

    }
}
    try {
            String url="http://172.17.59.97/post_songs?title="+file_name;
            URL obj=new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            //add reuqest header
            con.setRequestMethod("POST");

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            /*BufferedReader r = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = r.readLine()) != null) {
                wr.writeBytes(line+'\n');
            }*/
            //wr.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + uri.toString() +"\"" + "\r\n");

            byte []buffer = new byte[4096];
            int read = 0;

            while ( (read = is.read(buffer)) != -1 ) {
                wr.write(buffer, 0, read);
            }
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            Log.v("HTTPDelete_Check3", "Get returned: " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result

        }catch(java.io.IOException ex) {

        }