SMB protocol to Http for online video playback

1. Description

Why is cold knowledge? Because most projects are not needed, it is classified as cold knowledge.
This paper mainly describes how to convert the SMB protocol to the common HTTP protocol in order to use various players to play online.This article and the previous " Use SMB protocol to view computer files or other storage devices remotely There is an association.

2. Resources used:

Cybergarage.jar (this is a long time ago, the specific address can't be found. Search for it yourself)

3. Core Code

PlayFileService.java, needs to be registered in the main configuration file and open the change service when the application starts or needs to be converted.

package com.powerstick.service;

import org.cybergarage.http.HTTPServerList;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class PlayFileService extends Service {

    private FileServer fileServer = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        fileServer = new FileServer();
        fileServer.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        HTTPServerList httpServerList = fileServer.getHttpServerList();
        httpServerList.stop();
        httpServerList.close();
        httpServerList.clear();
        fileServer.interrupt();

    }
}

FileServer.java, this thread needs to be started in the transformation service.

package com.powerstick.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLDecoder;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import org.cybergarage.http.HTTPRequest;
import org.cybergarage.http.HTTPRequestListener;
import org.cybergarage.http.HTTPResponse;
import org.cybergarage.http.HTTPServerList;
import org.cybergarage.http.HTTPStatus;

import com.powerstick.BaseApplication;
import com.powerstick.utils.FileUtils;

public class FileServer extends Thread implements HTTPRequestListener {

    public static final String CONTENT_EXPORT_URI = "/smb";
    private HTTPServerList httpServerList = new HTTPServerList();
    // Default Shared Port
    private int HTTPPort = 2222;
    // Bound ip
    private String bindIP = null;

    public String getBindIP() {
        return bindIP;
    }

    public void setBindIP(String bindIP) {
        this.bindIP = bindIP;
    }

    public HTTPServerList getHttpServerList() {
        return httpServerList;
    }

    public void setHttpServerList(HTTPServerList httpServerList) {
        this.httpServerList = httpServerList;
    }

    public int getHTTPPort() {
        return HTTPPort;
    }

    public void setHTTPPort(int hTTPPort) {
        HTTPPort = hTTPPort;
    }

    @Override
    public void run() {
        super.run();

        /**************************************************
         * 
         * Create an http server to receive sharing requests
         * 
         *************************************************/
        // retry count
        int retryCnt = 0;
        // Get port 2222
        int bindPort = getHTTPPort();

        HTTPServerList hsl = getHttpServerList();
        while (hsl.open(bindPort) == false) {
            retryCnt++;
            // Return if the number of retries is greater than the number of server retries
            if (100 < retryCnt) {
                return;
            }
            setHTTPPort(bindPort + 1);
            bindPort = getHTTPPort();
        }
        // Add an HTTPRequestListener object to each HTTPServer object in the collection
        hsl.addRequestListener(this);
        // Call the start method of all HTTPServer s in the collection
        hsl.start();

        FileUtils.ip = hsl.getHTTPServer(0).getBindAddress();
        FileUtils.port = hsl.getHTTPServer(0).getBindPort();

    }

    @Override
    public void httpRequestRecieved(HTTPRequest httpReq) {

        String uri = httpReq.getURI();
        System.out.println("uri*****" + uri);

        if (uri.startsWith(CONTENT_EXPORT_URI) == false) {
            httpReq.returnBadRequest();
            return;
        }
        try {
            uri = URLDecoder.decode(uri, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        System.out.println("uri=====" + uri);
        if (uri.length() < 6) {
            return;
        }
        // Intercept file information
        String filePaths = "smb://" + uri.substring(5);

        System.out.println("filePaths=" + filePaths);
        // Determining if uri contains parameters
        int indexOf = filePaths.indexOf("&");

        if (indexOf != -1) {
            filePaths = filePaths.substring(0, indexOf);
        }

        try {
            SmbFile file = new SmbFile(filePaths,
                    BaseApplication.getInstance().getAuthentication());
            // Get the size of the file
            long contentLen = file.length();
            // Get File Type
            String contentType = FileUtils.getMIMEType(file.getName());
            System.out.println("contentType=====" + contentType);
            // Get Text File Stream
            InputStream contentIn = file.getInputStream();

            if (contentLen <= 0 || contentType.length() <= 0
                    || contentIn == null) {
                httpReq.returnBadRequest();
                return;
            }

            HTTPResponse httpRes = new HTTPResponse();
            httpRes.setContentType(contentType);
            httpRes.setStatusCode(HTTPStatus.OK);
            httpRes.setContentLength(contentLen);
            httpRes.setContentInputStream(contentIn);

            httpReq.post(httpRes);

            contentIn.close();
        } catch (MalformedURLException e) {
            // httpReq.returnBadRequest();
            return;
        } catch (SmbException e) {
            // httpReq.returnBadRequest();
            return;
        } catch (IOException e) {
            // httpReq.returnBadRequest();
            return;
        }
    }
}

*BaseApplication___

4. Turn on/off conversion services

Intent intent = new Intent(this, PlayFileService.class);
startService(intent);
Intent intent = new Intent(this, PlayFileService.class);
stopService(intent);

Other Related



Author: AFAP
Link: https://www.jianshu.com/p/e576c8df04bc
Source: Short Book
Copyright belongs to the author.For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.

Tags: Mobile Java Android

Posted on Sat, 08 Feb 2020 12:40:27 -0500 by spivey