使用REST API通过带有标头的多部分请求上传视频

时间:2017-11-17 06:56:14

标签: android multipart

我正在上传视频,它上传了标题请求,我尝试了请求属性,它不工作。但我不知道如何    使用此代码添加标头。  我需要在标头中发送带有api密钥的视频。 我在头请求中询问api密钥,而不是授权。

public int uploadFile(final String sourceFileUri) {
                HttpURLConnection conn = null;
                DataOutputStream dos = null;
                String lineEnd = "\r\n";
                String twoHyphens = "--";
                String boundary = "*****";
                String mm = "JJHJJHJHHKKJJKHHKHHKHKKKHHKHK";
                int bytesRead, bytesAvailable, bufferSize;
                byte[] buffer;
                int maxBufferSize = 1 * 1024 * 1024;
                File sourceFile = new File(sourceFileUri);
                if (!sourceFile.isFile()) {
                    return 0;
                } else {
                    try {
                        FileInputStream fileInputStream = new FileInputStream(sourceFile);
                        URL url = new URL("https:/fffs.co.in/ghh_site/api/video");
                        Log.d("WebService", "url=" + url);
                        // Open a HTTP connection to the URL
                        conn = (HttpURLConnection) url.openConnection();
                        conn.setDoInput(true); // Allow Inputs
                        conn.setDoOutput(true); // Allow Outputs
                        conn.setUseCaches(false); // Don't use a Cached Copy
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("Connection", "Keep-Alive");
                        conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                     //  conn.setRequestProperty("X-API-KEY=","GHJJHGJGHGHJGHHJGJGHGGJ");
                     //  conn.getHeaderFieldDate("X-API-KEY", Long.parseLong(mm));
                        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                        conn.setRequestProperty("video_upload", filepathUrl1.getName());

                    } catch (MalformedURLException ex) {
                        ex.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Log.d("ssk", "result" + result);
                    return serverResponseCode;
                } // End else block
            }

上面的代码没有标题,hw在multipart中添加它?

0 个答案:

没有答案