我正在编写代码以将音频文件从Android应用程序发送到服务器。连接运行良好,但我不知道如何使文件保存在服务器上。
我的问题是:如何将音频文件发送到服务器? 你能解释一下我到底应该一步一步做什么吗?我认为我在编码文件的某处错了..
public class UploadRecordingAsyncTask extends AsyncTask<String,Void, Void>{
String fileName;
String filePath;
String username;
public UploadRecordingAsyncTask(String filePath, String fileName, String username){
this.filePath = filePath;
this.fileName = fileName;
this.username = username;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
try{
URL url = new URL(SERVER_ADDRESS + "UploadRecording.php");
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
//past information
httpURLConnection.setDoOutput(true);
//get outputstreamwrite from http connection
OutputStream outputStream = httpURLConnection.getOutputStream();
//write down information
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, ENCODING_FORMAT));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//Encoding file part from here
FileInputStream fileInputStream = new FileInputStream(new File(filePath));
InputStream inputStreamFile = new BufferedInputStream(fileInputStream);
int numOfBytes = inputStreamFile.available();
byte[] audioBytesFile = new byte[numOfBytes];
int i = inputStreamFile.read(audioBytesFile,0,numOfBytes);
String audioString = Base64.encodeToString(audioBytesFile, 0);
inputStreamFile.close();
//encode data before sending
String data = URLEncoder.encode("filename", ENCODING_FORMAT) + "=" + URLEncoder.encode(fileName, ENCODING_FORMAT) + "&" +
URLEncoder.encode("owner", ENCODING_FORMAT) + "=" + URLEncoder.encode(username, ENCODING_FORMAT) + "&" +
URLEncoder.encode("encodedfile", ENCODING_FORMAT) + "=" + URLEncoder.encode(audioString, ENCODING_FORMAT) + "&";
//write data into buffer writer
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//input stream to get response from the server
InputStream inputStream = httpURLConnection.getInputStream();
inputStream.close();
httpURLConnection.disconnect();
}catch(MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
super.onPostExecute(aVoid);
}
}
在服务器上我有php文件:
<?php
require "init.php";
if(isset($_POST["encodedfile"])){
$filename = $_POST["filename"];
$owner = $_POST["owner"];
$decoded_string = base64_decode($_POST["encodedfile"]);
$path = "recordings/".$filename;
//new file of audio
$file = fopen($path, 'wb');
to_write_file = fwrite($file, $decoded_string);
fclose($file);
$query = "INSERT INTO Recording (filename, owner, encodedfile) VALUES (?,?,?)";
if($stmt = mysqli_prepare($connectDB, $query)){
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, 'sss', $filename, $owner, $decoded_string);
/* execute line*/
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
mysqli_close($connectDB);
}
?>