我可以将我的视频上传到我的服务器上,但是当我想上传一个变量以便在PHP中使用时,它就无法工作了。我做错了什么?我可以做些什么来上传变量然后上传视频,这样我就可以将视频命名为变量中的内容!
Android代码:
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");
OutputStream OS = conn.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("ProductAccountName", "UTF-8") + "=" + URLEncoder.encode(ProductOwnerEmail, "UTF-8") + "&" +
URLEncoder.encode("ProductName", "UTF-8") + "=" + URLEncoder.encode(ProductName, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = conn.getInputStream();
IS.close();
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);
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
$ProductOwnerEmail = $_POST['ProductOwnerEmail'];
$Productname= $_POST['Productname'];
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'];
$location = "/var/www/html/ProductVideos/$ProductOwnerEmail/" . $Productname;//$NewDirectory . '/' . $file_name;
move_uploaded_file($temp_name, $location);
}
?>
错误日志:
03-23 21:34:10.531 15083-15491/com.wilsapp.wilsapp W/System.err: java.io.IOException: closed
03-23 21:34:10.531 15083-15491/com.wilsapp.wilsapp W/System.err: at com.android.okhttp.okio.RealBufferedSink$1.write(RealBufferedSink.java:196)
03-23 21:34:10.531 15083-15491/com.wilsapp.wilsapp W/System.err: at java.io.OutputStream.write(OutputStream.java:82)
03-23 21:34:10.531 15083-15491/com.wilsapp.wilsapp W/System.err: at java.io.DataOutputStream.writeBytes(DataOutputStream.java:156)
03-23 21:34:10.531 15083-15491/com.wilsapp.wilsapp W/System.err: at com.wilsapp.wilsapp.SellerHomePage$ProductUpload.doInBackground(SellerHomePage.java:910)
03-23 21:34:10.531 15083-15491/com.wilsapp.wilsapp W/System.err: at com.wilsapp.wilsapp.SellerHomePage$ProductUpload.doInBackground(SellerHomePage.java:740)
03-23 21:34:10.531 15083-15491/com.wilsapp.wilsapp W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
03-23 21:34:10.532 15083-15491/com.wilsapp.wilsapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-23 21:34:10.532 15083-15491/com.wilsapp.wilsapp W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
03-23 21:34:10.532 15083-15491/com.wilsapp.wilsapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
03-23 21:34:10.532 15083-15491/com.wilsapp.wilsapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
03-23 21:34:10.532 15083-15491/com.wilsapp.wilsapp W/System.err: at java.lang.Thread.run(Thread.java:818)
03-23 21:34:10.575 776-1345/? W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@80a63f attribute=null, token = android.os.BinderProxy@5bc159
谢谢!