【Android 冷知識】SMB協議轉Http,實現視頻在線播放

1.說明

爲何說是冷知識,由於大多數項目用不到,因此歸類爲冷知識。
  本文主要介紹如何將SMB協議轉換爲常見的HTTP協議,以便利用各類播放器實如今線播放。本文與前篇《利用SMB協議遠程查看電腦文件或者其餘存儲設備》存在關聯。java

2.用到的資源:

cybergarage.jar(這個作很久了,具體地址找不到了,本身搜搜看看吧)android

3.核心代碼

PlayFileService.java,須要在主配置文件中註冊,並在應用啓動或者須要轉換的時候開啓改服務。服務器

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,須要在轉換服務中啓動該線程。app

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();
    // 默認的共享端口
    private int HTTPPort = 2222;
    // 綁定的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();

        /************************************************** * * 建立http服務器,接收共享請求 * *************************************************/
        // 重試次數
        int retryCnt = 0;
        // 獲取端口 2222
        int bindPort = getHTTPPort();

        HTTPServerList hsl = getHttpServerList();
        while (hsl.open(bindPort) == false) {
            retryCnt++;
            // 重試次數大於服務器重試次數時返回
            if (100 < retryCnt) {
                return;
            }
            setHTTPPort(bindPort + 1);
            bindPort = getHTTPPort();
        }
        // 給集合中的每一個HTTPServer對象添加HTTPRequestListener對象
        hsl.addRequestListener(this);
        // 調用集合中全部HTTPServer的start方法
        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;
        }
        // 截取文件的信息
        String filePaths = "smb://" + uri.substring(5);

        System.out.println("filePaths=" + filePaths);
        // 判斷uri中是否包含參數
        int indexOf = filePaths.indexOf("&");

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

        try {
            SmbFile file = new SmbFile(filePaths,
                    BaseApplication.getInstance().getAuthentication());
            // 獲取文件的大小
            long contentLen = file.length();
            // 獲取文件類型
            String contentType = FileUtils.getMIMEType(file.getName());
            System.out.println("contentType=====" + contentType);
            // 獲取文文件流
            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的,能夠在前篇文章中看到;FileUtils中定義了靜態變量ip="127.0.0.1",port=0,其餘的就用到了根據文件名稱獲取MIME類型的方法,因爲方法簡單且代碼太長就不貼了ide

4.開啓/關閉轉換服務

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

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

其餘相關

做者:AFAP 連接:https://www.jianshu.com/p/e576c8df04bc 來源:簡書 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
相關文章
相關標籤/搜索