更新答案在这里:) https://stackoverflow.com/a/23648537/6042879
我希望能够将我的视频和变量上传到我的服务器,以便在PHP脚本中使用。
到目前为止,我可以从手机中选择我想要的视频并将其完美地上传到服务器,但无法弄清楚如何通过视频发送变量。我可以单独完成它们,但如果我将它们组合起来它就无法工作。
我使用此代码来上传变量:
//Uploads the product details
try {//Try block is to see if the call to the database can work.
URL url = new URL(ProductDetails_URL);//Create a new URL and put there variable "register_URL" into it.
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//Create a httpConnection and open it
httpURLConnection.setRequestMethod("POST");//Use the request method
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("ProductOwnerEmail", "UTF-8") + "=" + URLEncoder.encode(ProductOwnerEmail, "UTF-8") + "&" +
URLEncoder.encode(DescriptionPoint3, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) { //url.openConnection() catch statement
e.printStackTrace();
}
视频上传代码:
try{
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(UploadVideo_URL);
conn = (HttpURLConnection) url.openConnection();
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", selectedPath);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + selectedPath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"Details\";Email=\"" + ProductOwnerEmail + "\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.write(ProductOwnerEmail.getBytes());
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"Details\";KeyCode=\"" + ProductKeyCode + "\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.write(ProductOwnerEmail.getBytes());
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();
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception e) {
e.printStackTrace();
return "Product upload failed";
}
PHP代码:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$file_name = $_FILES['myFile']['name'];
$file_size = $_FILES['myFile']['size'];
$file_type = $_FILES['myFile']['type'];
$temp_name = $_FILES['myFile']['tmp_name'];
$ProductOwnerEmail = $_FILES['Details']['Email'];
$ProductKeyCode = $_FILES['Details']['KeyCode'];
$NewDirectory = "/var/www/html/ProductVideos/" . $ProductOwnerEmail;
if (!file_exists($NewDirectory))
{
mkdir($NewDirectory, 0777, true);
}
$location = "/var/www/html/ProductVideos/$ProductOwnerEmail/" . $ProductKeyCode;//$NewDirectory . '/' . $file_name;
move_uploaded_file($temp_name, $location);
echo "Uploaded!";
}else{
echo "Error";
}
?>
答案 0 :(得分:1)
编码后:
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + SelectedPDF + "\"" + lineEnd);
dos.writeBytes(lineEnd);
写下面的内容来发送变量:
dos.writeBytes(LINE_END);
// Loop a list of variable that you want to send to server.
/*for (StringKeyValuePair pair : yourVariableList) {
dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"" + pair.getKey()+ "\"" + LINE_END);
dos.writeBytes(LINE_END);
dos.write(pair.getValue().getBytes());
dos.writeBytes(LINE_END);
}*/
dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"ProductOwnerEmail\"" + LINE_END);
dos.writeBytes(LINE_END);
dos.write(ProductOwnerEmail.getBytes());
dos.writeBytes(LINE_END);
dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"ProductKeyCode\"" + LINE_END);
dos.writeBytes(LINE_END);
dos.write(ProductKeyCode.getBytes());
dos.writeBytes(LINE_END);
答案 1 :(得分:0)
你可以使用Volley lib来达到这个目的。它还可以将您的请求保留在队列中并且易于使用http://developer.android.com/training/volley/index.html