首页 > 解决方案 > 音频文件上传到android中的实时服务器

问题描述

当我尝试运行以下代码时,它没有显示任何错误。它的日志“文件Witten”。但文件未上传到服务器。虽然我在本地尝试相同的代码,但 PHP 代码能够上传。请帮助。在此先感谢

包 com.example.file_upload_demo;导入android.os.Handler;导入android.os.Message;enter code here 导入android.util.Log;导入 java.io.DataInputStream;导入 java.io.DataOutputStream;导入java.io.File;导入 java.io.FileInputStream;导入 java.io.IOException;导入 java.net.HttpURLConnection;导入 java.net.MalformedURLException;导入 java.net.URL;公共类 FileUploadUtility { 静态字符串 SERVER_PATH = "example.com";

enter code here
    public static void doFileUpload(final String selectedPath, final Handler handler) {
//
        new Thread(new Runnable() {
            @Override
            public void run() {`enter code here`
                HttpsTrustManager.allowAllSSL();
                HttpURLConnection conn = null;
                DataOutputStream dos = null;
                DataInputStream inStream = null;
                String lineEnd = "rn";
                String twoHyphens = "--";
                String boundary = "*****";
                int bytesRead, bytesAvailable, bufferSize;
                byte[] buffer;
                int maxBufferSize = 1 * 1024 * 1024;
                String responseFromServer = "";
                try {
                    //------------------ CLIENT REQUEST
                    FileInputStream fileInputStream = new FileInputStream(new File(selectedPath));

                    // open a URL connection to the Servlet
                    URL url = new URL(SERVER_PATH);
                    // Open a HTTP connection to the URL
                    conn = (HttpURLConnection) url.openConnection();
                    // Allow Inputs
                    conn.setDoInput(true);
                    // Allow Outputs
                    conn.setDoOutput(true);
                    // Don't use a cached copy.
                    conn.setUseCaches(false);
                    // Use a post method.
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("Content-Type", "application/json;charset=" + boundary);
                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"mp3File\";filename=\""
                            + selectedPath + "\"" + 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);
                    // close streams
                    Log.e("Debug", "File is written");
                    fileInputStream.close();
                    dos.flush();
                    dos.close();

                } catch (MalformedURLException ex) {
                    Log.e("Debug", "error: " + ex.getMessage(), ex);
                    sendMessageBack(responseFromServer, 0, handler);
                    return;
                } catch (IOException ioe) {
                    Log.e("Debug", "error: " + ioe.getMessage(), ioe);
                    sendMessageBack(responseFromServer, 0, handler);
                    return;
                }
                responseFromServer = processResponse(conn, responseFromServer);
                sendMessageBack(responseFromServer, 1, handler);
            }
        }).start();

    }

    private static String processResponse(HttpURLConnection conn, String responseFromServer) {
        DataInputStream inStream;
        try {
            inStream = new DataInputStream(conn.getInputStream());
            String str;

            while ((str = inStream.readLine()) != null) {
                responseFromServer = str;
            }
            inStream.close();

        } catch (IOException ioex) {
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        }
        return responseFromServer;
    }

    static void sendMessageBack(String responseFromServer, int success, Handler handler) {
        Message message = new Message();
        message.obj = responseFromServer;
        message.arg1 = success;
        handler.sendMessage(message);
    }`enter code here`
}

标签: androidfilefile-upload

解决方案


试试这个:1)在你的应用程序中打开清单文件夹

2)添加

<uses-permission android:name="android.permission.INTERNET" />

3) 添加<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />


推荐阅读