Hello, I'm trying to send a video to the server: Here is the class with which I work:
public class Upload { public static final String UPLOAD_URL= "http://simplifiedcoding.16mb.com/VideoUpload/upload.php"; private int serverResponseCode; public String uploadVideo(String file) { String fileName = file; HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; File sourceFile = new File(file); if (!sourceFile.isFile()) { Log.e("Huzza", "Source File Does not exist"); return null; } try { FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(UPLOAD_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", 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(); 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"; } } I have an error when sending to the server, I suspect that I incorrectly indicated the path to the php file (I tried it again through the IP). PCP lies in the -WAMP / www / VideoUpload / upload.php folder and here is the script itself:
<?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']; $location = "uploads/"; move_uploaded_file($temp_name, $location.$file_name); echo "http://xxx/VideoUpload/uploads/".$file_name; }else{ echo "Error"; } Help me please!
https://simplifiedcoding.16mb.com/VideoUpload/upload.php- newakkoff