duandongji2231 2018-04-17 06:46
浏览 57

将WAV文件发布到服务器失败 - $ _FILE显示Array() - Kotlin,Android,PHP

I tried to build some speech recognition app on Android, using kaldi(Audio Speech Recognition software) on local server. I'd like to post WAV file to local server. I found this article:

How to upload a WAV file using URLConnection

and followed & with some other references, I wrote a code but it doesn't work properly.

I got the sentence Array() as $_FILE in php. I think the way to POST binary is wrong, but I can't figure out what's wrong...

My MainActivity.kt (part)

private class UploadFileTask : AsyncTask<Void, Void, Void>(){
    override fun doInBackground(vararg params: Void): Void? {
        var con: HttpURLConnection? = null
        try {
            Log.i("POST","Start POST")
            con = POST_URL.openConnection() as HttpURLConnection
            con.connectTimeout = 300000
            con.setRequestProperty("Accept-Charset", "UTF-8")
            con.setRequestProperty("Connection", "Keep-Alive")
            con.setRequestProperty("Cache-Control", "no-cache")
            con.setRequestProperty("Content-Type", "audio/wav")
            con.requestMethod = "POST"
            con.instanceFollowRedirects = false
            con.doInput = true
            con.doOutput = true
            val wavFile = File(wavPath)
            var postData: ByteArray? = null
            if (wavFile.exists()) {
                Log.i("FILE", "kaldi.wav detected")
                postData = wavFile.readBytes()
            }

            if (postData != null) {
                con.setRequestProperty("charset", "utf-8")
                con.setRequestProperty("Content-length", postData.size.toString())
                Log.i("POST", "exec post")

                con.connect()

                try {
                    val outputStream = DataOutputStream(con.outputStream)
                    outputStream.write(postData)
                    outputStream.flush()
                    outputStream.close()
                } catch (e: Exception) {
                    Log.e("POST", "Exception occurred!")
                }
                val status = con.responseCode
                Log.i("STATUS", Integer.toString(status))

                when (status) {
                    HttpURLConnection.HTTP_OK -> {
                        val inputStream = con.inputStream
                        val reader = BufferedReader(InputStreamReader(inputStream))
                        var httpSource = ""
                        var str = reader.readLine()
                        while (str != null){
                            httpSource += str
                            str = reader.readLine()
                        }
                        Log.i("HTTPSOURCE", "source:$httpSource")
                        inputStream.close()
                    } HttpURLConnection.HTTP_UNAUTHORIZED ->{
                        Log.e("POST", "UNAUTHORIZED")
                    } else -> {
                        Log.i("POST", "CODE IS " + Integer.toString(status))
                    }
                }

            }
            con.disconnect()

        } catch (e: InterruptedException){
            Log.e("UPLOADER", "aborting.")
        } finally {
            con?.disconnect()
        }
        return null
    }

And post.php

<?php

$uploaddir = '/var/www/html/files/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "OK UPLOADED";
} else {
    echo "ERROR";
}
print_r($_FILES);


?>
  • 写回答

1条回答 默认 最新

  • doupeng3501 2018-04-18 03:03
    关注

    Finally, the code below works. I use multipart post solution. Now I think that kotlin's readBytes doesn't work properly with OutputDataStream , but I'm not sure about it...

    private class UploadFileTask : AsyncTask<Void, Void, Void>() {
        override fun doInBackground(vararg params: Void): Void? {
            var con: HttpURLConnection? = null
            try {
                Log.i("POST", "Start POST")
                con = POST_URL.openConnection() as HttpURLConnection
                con.connectTimeout = 30 * 1000
                con.setRequestProperty("Accept-Charset", "UTF-8")
                con.setRequestProperty("ENCTYPE", "multipart/form-data")
                con.setRequestProperty("Connection", "Keep-Alive")
                con.setRequestProperty("Cache-Control", "no-cache")
                con.setRequestProperty("Content-Type", "multipart/form-data; boundary=$boundary")
                con.requestMethod = "POST"
                con.instanceFollowRedirects = false
                con.doInput = true
                con.doOutput = true
                val wavFile = File(wavPath)
                Log.i("POST", "exec post")
    
                con.connect()
    
                try {
                    Log.i("POST", "Now writing to stream...")
                    val outputStream = DataOutputStream(con.outputStream)
                    outputStream.writeBytes(twoHyphens + boundary + lineEnd)
                    outputStream.writeBytes("Content-Disposition: form-data; name=\"post\"; filename=\"${wavFile.name}\"$lineEnd")
                    outputStream.writeBytes("Content-Type: application/octet-stream$lineEnd")
                    outputStream.writeBytes("Content-Transfer-Encoding: binary$lineEnd")
                    outputStream.writeBytes(lineEnd)
                    val inputStream = FileInputStream(wavFile)
                    inputStream.copyTo(outputStream)
                    outputStream.writeBytes(lineEnd)
                    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd)
                    outputStream.flush()
                    outputStream.close()
    
                } catch (e: Exception) {
                    Log.e("POST", "Exception occurred!")
                }
                val status = con.responseCode
                Log.i("STATUS", Integer.toString(status))
    
                when (status) {
                    HttpURLConnection.HTTP_OK -> {
                        val inputStream = con.inputStream
                        val reader = BufferedReader(InputStreamReader(inputStream))
                        var httpSource = ""
                        var str = reader.readLine()
                        while (str != null) {
                            httpSource += str
                            str = reader.readLine()
                        }
                        Log.i("HTTPSOURCE", "source:$httpSource")
                        inputStream.close()
                    }
                    HttpURLConnection.HTTP_UNAUTHORIZED -> {
                        Log.e("POST", "UNAUTHORIZED")
                    }
                    else -> {
                        Log.i("POST", "CODE IS " + Integer.toString(status))
                    }
    
                }
    
                con.disconnect()
    
            } catch (e: InterruptedException) {
                Log.e("UPLOADER", "aborting.")
            } finally {
                con?.disconnect()
            }
            return null
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable