捕获视频并从android上传到服务器

捕获视频并从android上传到服务器,android,Android,我想捕获视频并上传到服务器,我已经完成了捕获视频的代码,并将其显示到视频视图,它运行成功,我想扩展它以将捕获的视频上传到服务器。 我正在使用asp.NETWeb服务来实现这一点。 这是我的密码 @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { // After camera screen this code will excute

我想捕获视频并上传到服务器,我已经完成了捕获视频的代码,并将其显示到视频视图,它运行成功,我想扩展它以将捕获的视频上传到服务器。 我正在使用asp.NETWeb服务来实现这一点。 这是我的密码

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        // After camera screen this code will excuted

        if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {

            if (resultCode == RESULT_OK) {
             VideoView mVideoView=(VideoView)findViewById(R.id.mVideoView);
                Uri videoUri = intent.getData();



                MediaController  mc = new MediaController(this);
                mVideoView.setMediaController(mc);
                mVideoView.requestFocus();
                mVideoView.setVideoURI(videoUri);

                mc.show();
                mVideoView.setOnPreparedListener(new OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        // TODO Auto-generated method stub
                        mp.start();
                        mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                            @Override
                            public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                                    int arg2) {

                                mp.start();
                            }
                        });

                    }
                });

                sourceFileUri=getPath(videoUri);
                try {

                    upLoad2Server();

                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    private String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION}; 
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        cursor.moveToFirst(); 
        String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
        int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
        long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));


        //some extra potentially useful data to help with filtering if necessary
       // System.out.println("size: " + fileSize);
        //System.out.println("path: " + filePath);
       // System.out.println("duration: " + duration);

        return filePath;
    }
private int serverResponseCode;
String sourceFileUri;
    public void upLoad2Server()
    {



        AsyncTask<Void, Void, Void> updateTask = new AsyncTask<Void, Void, Void>(){
            ProgressDialog dialog = new ProgressDialog(VideoCaptureActivity.this);
            @Override
            protected void onPreExecute() {
                // what to do before background task
                dialog.setTitle("Loading");
                dialog.setMessage("Please wait...");
                dialog.setIndeterminate(true);
                dialog.setCancelable(false);
                dialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {


             String upLoadServerUri = "server webservice url";
             // String [] string = sourceFileUri;
             String fileName = sourceFileUri;

             HttpURLConnection conn = null;
             DataOutputStream dos = null;
             DataInputStream inStream = null;
             String lineEnd = "\r\n";
             String twoHyphens = "--";
             String boundary = "*****";
             int bytesRead, bytesAvailable, bufferSize;
              byte[] buffer;
             int maxBufferSize = 1 * 1024 * 1024;
             String responseFromServer = "";

             File sourceFile = new File(sourceFileUri);

            try { // open a URL connection to the Servlet
             FileInputStream fileInputStream = new FileInputStream(sourceFile);
             URL url = new URL(upLoadServerUri);
             conn = (HttpURLConnection) url.openConnection(); // Open a HTTP  connection to  the URL 
             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("Content-Type", "multipart/form-data;boundary=" + boundary);
             conn.setRequestProperty("uploaded_file", fileName);
            dos = new DataOutputStream(conn.getOutputStream());

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
             dos.writeBytes(lineEnd);

             bytesAvailable = fileInputStream.available(); // create a buffer of  maximum size
             Log.i("Huzza", "Initial .available : " + bytesAvailable);

             bufferSize = Math.min(bytesAvailable, maxBufferSize);
             buffer = new byte[bufferSize];

             // read file and write it into form...
             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);
              }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

             // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("Upload file to server", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
            // close streams
            Log.i("Upload file to server", fileName + " File is written");
             fileInputStream.close();
            dos.flush();
            dos.close();
             } catch (MalformedURLException ex) {
             ex.printStackTrace();
            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
            } catch (Exception e) {
            e.printStackTrace();
             }
            //this block will give the response of upload link
              try {
              BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                .getInputStream()));
              String line;
              while ((line = rd.readLine()) != null) {
               Log.i("Huzza", "RES Message: " + line);
              }
              rd.close();
             } catch (IOException ioex) {
              Log.e("Huzza", "error: " + ioex.getMessage(), ioex);
             }
              //Toast.makeText(this,"Video Uplaoded to server", 2000);


              return null;
            }
            @Override
            protected void onPostExecute(Void result) {

                dialog.dismiss();

            };

            @Override
            protected void onCancelled() {
                dialog.dismiss();
                super.onCancelled();
            }
            @Override
            protected void onProgressUpdate(Void... args){
              //dialog.
              //dialog.setProgress(args[0]);
            }
        };

        updateTask.execute((Void[])null);

    } // end upLoad2Server
}

使用此文件通过ftp上传

SimpleFTP ftp = new SimpleFTP();


            // Connect to an FTP server on port 21.
            ftp.connect("server address", 21, "username", "pwd");

            // Set binary mode.
            ftp.bin();

            // Change to a new working directory on the FTP server.


            ftp.cwd("path");

            // Upload some files.
            ftp.stor(new File("your-file-path"));              

            // Quit from the FTP server.
            ftp.disconnect();
有关更多信息和jar