【问题标题】:android upload video and string to serverandroid将视频和字符串上传到服务器
【发布时间】:2016-06-10 23:34:10
【问题描述】:

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

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();

【问题讨论】:

    标签: php android video upload httpurlconnection


    【解决方案1】:

    添加

    conn.setRequestProperty("stringName", string);
    

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

    【讨论】:

    • 谢谢,我想通过 DoInBackground 方法将字符串发送到uploadvideo();
    猜你喜欢
    • 2011-06-28
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2013-12-30
    相关资源
    最近更新 更多