安卓上传视频和字符串到服务器


android upload video and string to server

我目前拥有的代码会将视频上传到服务器,但我不确定如何将字符串与视频一起添加到服务器。我想发送视频,然后发送一个字符串以发送到上传。

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";
    }
}

上传活动

@Override
        protected String doInBackground(Void... params) {
            Upload u = new Upload();
            String msg = u.uploadVideo(selectedPath);
            return msg;
        }
    }
    UploadVideo uv = new UploadVideo();
    uv.execute();

添加

conn.setRequestProperty("stringName", string);

毕竟conn.setRequestProperty然后在服务器上确保你收到它(在 PHP 上它是 $varname = $_POST['stringName'];