从Android向服务器发送mp3文件时遇到困难



我正试图将一个mp3文件从我的Android应用程序发送到服务器。我使用这个方法android异步http发送mp3文件。

这是我的密码。

            try {
                params1.put("file", new ByteArrayInputStream(MeetingFragment.mediafile));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            AsyncClientHandler.post("events/Uploadrecording/5", params1,
                    new AsyncHttpResponseHandler() {
                        @Override
                        public void onFailure(int statusCode,
                                Throwable error, String content) {
                            // TODO Auto-generated method stub
                            super.onFailure(statusCode, error, content);
                            Log.i("filename",myFile.toString());
                            Toast.makeText(_context, "failiure push"+ProjectEventFragment.event.eve_id, Toast.LENGTH_SHORT).show();
                        }
                        @SuppressWarnings("deprecation")
                        @Override
                        public void onSuccess(int statusCode,
                                Header[] headers, String content) {
                            // TODO Auto-generated method stub
                            super.onSuccess(statusCode, headers, content);
                            Toast.makeText(_context, "success push"+ProjectEventFragment.event.eve_id, Toast.LENGTH_SHORT).show();
                            Log.i("Successpush",content.toString());
                        }                   
                    });

我已经给出了正确的URL,我也得到了文件的值。但它将成为成功的一部分。有人能帮我解决这个问题吗?如果你知道其他方法,请建议我。提前谢谢。

public static byte[] mediafile;
public static String record_file=null
 myAudioRecorder = new MediaRecorder();
      myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
      myAudioRecorder.setOutputFile(record_file);
      mediafile = record_file.getBytes();

Java类

class UpLoadFiles extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ProfileActivity.this);
        pDialog.setMessage("Loading profile ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    /**
     * getting Profile JSON
     * */
    protected String doInBackground(String... args) {
        String sourceFileUri = "/mnt/sdcard/a.mp3";
    System.out.println("file path:" + sourceFileUri);
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "rn";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        File sourceFile = new File(sourceFileUri);
        ////System.out.println("file exists:" + sourceFile + " Email " + email);
        try {
            String upLoadServerUri ="http:/pairdroid.com/fileupload.php?";
            String filenameArray[] = sourceFile.getName().split("\.");
            String extension = filenameArray[filenameArray.length - 1];
            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(
                    sourceFile);
            URL url = new URL(upLoadServerUri);
            // Open a HTTP connection to the URL
            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("bill", sourceFileUri);
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name="bill";filename=""
                    + sourceFileUri + """ + 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);
            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();
            if (serverResponseCode == 200) {
                ////System.out.println("Uploaded http: 200//");
                // messageText.setText(msg);
                // Toast.makeText(ctx, "File Upload Complete.",
                // Toast.LENGTH_SHORT).show();
                // recursiveDelete(mDirectory1);
            }
            // close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (Exception e) {
            // dialog.dismiss();
            e.printStackTrace();
        }
        // dialog.dismiss();
        return "Executed";
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
    }
}

php代码(fileupload.php)

 <?php
  $uploads_dir = './';
  $tmp_name = $_FILES['bill']['tmp_name'];
  $pic_name = $_FILES['bill']['name'];
  if (move_uploaded_file($tmp_name, $uploads_dir.$pic_name )) {
  }           
 ?>

相关内容

  • 没有找到相关文章

最新更新