我正在开发一个模块,用于录制语音,然后使用MultiPartEntity上传到php服务器。
我能够在sdcard中录制语音及其保存文件,但每当我尝试将该文件上传到PHP服务器(使用MPE)时,它就不会上传和服务器获取空白文件内容。
在我的设备中,我能够播放该声音并在设备中正常工作。只是上传问题。我使用相同的代码上传其他媒体,如图像和视频和其他文件。一切都很好。只记录录制的文件。
这是我录制语音的代码。
MediaRecorder recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
String filePath = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/abc.mp4";
recorder.setOutputFile(filePath); // This is my file path to store data
recorder.prepare();
recorder.start();
我正在使用multipart在服务器上上传此录音。
这是我的MultiPartEntity代码。
public class MultipartUtility {
private static final String LINE_FEED = "\r\n";
private static final int TIMEOUT = 60000;
private final String boundary;
private HttpURLConnection httpConn;
private OutputStream outputStream;
private PrintWriter writer;
public MultipartUtility(String requestURL) throws IOException {
boundary = "======" + System.currentTimeMillis() + "======";
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // Indicates POST Method
httpConn.setDoInput(true);
httpConn.setReadTimeout(TIMEOUT);
httpConn.setConnectTimeout(TIMEOUT);
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("Connection", "Keep-Alive");
// ================
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
}
public void addFormField(String name, String value) {
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"").append(name).append("\"").append(LINE_FEED);
//writer.append("Content-Type: text/plain; charset=UTF-8").append(LINE_FEED);
writer.append(LINE_FEED).append(value).append(LINE_FEED);
writer.flush();
}
public void addFilePart(String fieldName, File uploadFile) throws IOException {
String fileName = uploadFile.getName();
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"").append(fieldName).append("\"; filename=\"").append(fileName).append("\"").append(LINE_FEED);
writer.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED).flush();
}
public String execute() throws IOException {
String response = "";
writer.append(LINE_FEED).flush();
writer.append("--").append(boundary).append("--").append(LINE_FEED);
writer.close();
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
response += line;
}
reader.close();
} else {
return null;
}
httpConn.disconnect();
return response;
}
我在网上搜索了同一主题,但没有得到针对此问题的exect解决方案。
请帮帮我朋友。 Thanx in Advance