我想把一个音频文件从Android项目传递到Java中的动态Web应用项目。现在,我想在服务器中创建一个文件夹来保存这个音频文件,而不是将数据库创建为后端。有谁能帮我在服务器的文件夹里保存一个.mp3文件…我的代码如下

package com.android.audio.mediaplayer;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.android.qrcode.db.AudioVideoServer;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;



public class AudioRecord  extends Activity
{
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;
    public String AudioFile;

    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;

    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;
    private SubmitButton mSubmitButton = null;
    private String url = "postLoginData.do?do=postLoginData";
    String result;
    byte[] value;
    String s;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }
    /* public boolean saveas(int ressound){
             byte[] buffer=null;
             InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
             int size=0;

             try {
              size = fIn.available();
              buffer = new byte[size];
              fIn.read(buffer);
             fIn.close();
            } catch (IOException e) {
             // TODO Auto-generated catch block
            return false;
            }

            String path="/sdcard/media/audio/ringtones/";
            String filename="examplefile"+".ogg";

            boolean exists = (new File(path)).exists();
            if (!exists){new File(path).mkdirs();}

            FileOutputStream save;
            try {
             save = new FileOutputStream(path+filename);
             save.write(buffer);
             save.flush();
             save.close();
            } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             return false;
            } catch (IOException e) {
             // TODO Auto-generated catch block
             return false;
            }

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));

            File k = new File(path, filename);

            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "exampletitle");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
            values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, false);

            //Insert it into the database
            this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);


            return true;
           }  */
    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            setOnClickListener(clicker);
        }
    }
    class SubmitButton extends Button {

        OnClickListener clicker = new OnClickListener() {

            public void onClick(View v) {

                byte[] file = mFileName.getBytes();
                System.out.println("$$$$$$$$$$$" + file);
                s = Base64.encodeToString(file, MODE_APPEND);
                System.out.println("**************" + s);

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("Audiofile.mp3", s));
                result = AudioVideoServer.executePost(url, nameValuePairs);

            }
        };

        public SubmitButton(Context ctx) {
            super(ctx);
            setText("Save");
            setOnClickListener(clicker);
        }
    }

    public AudioRecord() {
        Bundle b = getIntent().getExtras();
         //mImagePath = b.getString("path");
        AudioFile = b.getString("audiofile");

        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName +=AudioFile;
            //"/audiorecordtest.mp4";
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        mPlayButton = new PlayButton(this);
        ll.addView(mPlayButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        mSubmitButton = new SubmitButton(this);
        ll.addView(mSubmitButton, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0));


        setContentView(ll);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

最佳答案

在JavaEE应用程序中实现文件上传功能的一种简单方法是利用具有文件上传的extension框架的RestLeT框架。此扩展基于apache的FileUpload
您可以找到一些fileuploadherehere的示例。
如果您使用的是Tomcat7或任何其他Servlet3.0容器,那么文件上传功能是内置的。有关示例,请参见here

10-07 19:20
查看更多