我已经捕获了3MB视频并将其保存到我的Android设备SD卡,我从设备中选择它并想将其传输到php服务器。上传工作完美,但需要很长时间,甚至3mb视频上传需要4-5分钟,即使我也有快速的互联网连接。那么有没有办法更快地完成上传任务?或任何其他方式来做到这一点。非常感谢任何其他建议。在此先感谢:)
这是我的上传类连接和解析视频。
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(UPLOAD_URL);
conn = (HttpURLConnection) url.openConnection();
// conn.setFixedLengthStreamingMode(1024);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
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("myFile", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
Log.i("Huzza", "Initial .available : " + bytesAvailable);
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
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);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
Toast.makeText(_context,"response code is " +serverResponseCode,Toast.LENGTH_SHORT).show();
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (serverResponseCode == 200) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (IOException ioex) {
}
return sb.toString();
}else {
return "Could not upload";
}
}
}
这是我的后台任务的主要活动。
private void uploadVideo() {
class UploadVideo extends AsyncTask<Void, Void, String> {
ProgressDialog uploading;
@Override
protected void onPreExecute() {
super.onPreExecute();
uploading = ProgressDialog.show(MainActivity.this, "Uploading File", "Please wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
uploading.dismiss();
textViewResponse.setText(Html.fromHtml("<b>Uploaded at <a href='" + s + "'>" + s + "</a></b>"));
textViewResponse.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
protected String doInBackground(Void... params) {
Upload u = new Upload();
String msg = u.uploadVideo(selectedPath,getApplicationContext());
return msg;
}
}
UploadVideo uv = new UploadVideo();
uv.execute();
}