Java 上载以datetime和字符串作为名称的音频文件

Java 上载以datetime和字符串作为名称的音频文件,java,android,Java,Android,我正在将一个音频文件上载到服务器,当文件名类似于: abcd.3gp 问题是,每当我录制并上传另一个文件时,由于名称是常量,它会覆盖现有文件,而这不是我想要的。我希望总是有一个新文件。我也不想使用随机数或类似的东西 所以我现在要做的是,我有一个datetime格式,在这个格式中,我删除了它的所有-或/和:并将其存储在一个变量中 例如,如果我有这个日期: 2014-06-23 10:16:23 我将此转化为: 06232014_101623 然后我添加了一些额外的文本,得到如下内容: 062320

我正在将一个音频文件上载到服务器,当文件名类似于:

abcd.3gp

问题是,每当我录制并上传另一个文件时,由于名称是常量,它会覆盖现有文件,而这不是我想要的。我希望总是有一个新文件。我也不想使用随机数或类似的东西

所以我现在要做的是,我有一个datetime格式,在这个格式中,我删除了它的所有-或/和:并将其存储在一个变量中

例如,如果我有这个日期:

2014-06-23 10:16:23

我将此转化为:

06232014_101623

然后我添加了一些额外的文本,得到如下内容:

06232014_101623ABC.3gp

这是我想要的格式

问题是,当我录制时,它可以使用这个名称完美地工作和保存,但当它开始上载时,我会得到一个“未找到文件”异常,即使该文件存在并且存在

当我把名字改成像abcd.3gp这样的名字时,我没有例外,它会上传

这是记录的代码:

Date currentDateTimeString= new Date();
DateFormat sdf;
sdf = new SimpleDateFormat("MMddyyyy_hhmmss");
String  strDate = sdf.format(currentDateTimeString);
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/"+ strDate+TNumber+".3gp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

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

mRecorder.start();
这是用于上载的代码:

    Date currentDateTimeString= new Date();
    DateFormat sdf;
    sdf = new SimpleDateFormat("MMddyyyy_hhmmss");
    String  strDate = sdf.format(currentDateTimeString);

    // TODO Auto-generated method stub
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;       
    String existingFileName = 
    Environment.getExternalStorageDirectory().getAbsolutePath();
            existingFileName+= "/"+ strDate+TNumber+".3gp";

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;

    String urlString = "http://10.15.15.149/androidphp/Uploadaudio.php";

    try {

        //------------------ CLIENT REQUEST
        FileInputStream fileInputStream = 
        new FileInputStream(new File(existingFileName));
        // open a URL connection to the Servlet
        URL url = new URL(urlString);
        // 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", "multipart/form-data;boundary=" + boundary);
        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + 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);
    } catch (IOException ioe) {
        Log.e("Debug", "error: " + ioe.getMessage(), ioe);
    }

    //------------------ read the SERVER RESPONSE
    try {

        inStream = new DataInputStream(conn.getInputStream());
        String str;

        while ((str = inStream.readLine()) != null) {

            Log.e("Debug", "Server Response " + str);

        }

        inStream.close();

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

    return null;
}
如有任何线索或帮助,将不胜感激。谢谢。

我已经解决了我的问题。 问题在于currentDateTimeString的变量声明范围。因此,在录制音频文件之后,因为它是一个日期和时间,所以在开始上传之前,时间会发生变化。因此,在这种情况下,它将始终在字符串的其余部分附加一个不同的日期时间值,该值与保存的日期时间值不同。 所以我改变了变量的作用域,现在它始终保持相同的datetime值