簡體   English   中英

上傳音頻文件時使用HttpURLConnection傳遞參數

[英]Passing parameters with HttpURLConnection when Uploading audio file

當我從堆棧溢出答案中獲取代碼時。 發送.txt文件,將文件文件發送到android中的服務器

修改以下代碼,我正在嘗試將音頻/視頻文件發布到在線php服務器。 一切正常,文件正確上傳並保存在數據庫中,但問題是我還發送參數與文件,以便我可以識別哪個用戶正在發送記錄, (user_id和candidate_id和file_type)但在數據庫中他們的值為null / 0。

請有人告訴我,我的代碼有什么問題,為什么它不適用於params。 當我用POSTMAN檢查我的php端服務器時,所有值都正確保存文件。

try {
            FileInputStream fileInputStream = new FileInputStream(
                    sourceFile);
            URL url = new URL(Config.URL_UPLOAD);
            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("uploadedfile", fileName);

            dos = new DataOutputStream(conn.getOutputStream());
            //here am adding the params

            param.put("user_id", Constants.user_id);
            param.put("candidate_id", "candiidateid1");
            param.put("file_type", Constants.file_type);
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(dos, "UTF-8"));
            writer.write(getPostDataString(param));
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileName + "\"" + 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);

            dialog.dismiss();
            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("uploadFile", "HTTP Response is : "
                    + serverResponseMessage + ": " + serverResponseCode);
            DataInputStream inStream = null;
            if (serverResponseCode == 200) {
                try {
                    Log.e("isStream = ", "" + inStream);
                    inStream = new DataInputStream(conn.getInputStream());
                    String str;
                    Log.e("isStream = ", "" + inStream);
                    while ((str = inStream.readLine()) != null) {
                        Log.e("Debug", "Server Response " + str);
                    }
                    inStream.close();
                    Log.e("isStream = ", "" + inStream);
                } catch (IOException ioex) {
                    Log.e("Debug", "error: " + ioex.getMessage(), ioex);
                }

            }

            // close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {

            dialog.dismiss();
            ex.printStackTrace();


            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {

            dialog.dismiss();
            e.printStackTrace();


            Log.e("Upload file to server ",
                    "Exception : " + e.getMessage(), e);
        }

請參考以下網址,明確回答傳遞參數

使用帶HttpURLConnection的POST發送文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM