【Android 冷知識】SMB協(xié)議轉(zhuǎn)Http动遭,實現(xiàn)視頻在線播放

1.說明

為什么說是冷知識芬探,因為大多數(shù)項目用不到,所以歸類為冷知識厘惦。
  本文主要介紹如何將SMB協(xié)議轉(zhuǎn)換為常見的HTTP協(xié)議偷仿,以便利用各種播放器實現(xiàn)在線播放。本文與前篇《利用SMB協(xié)議遠(yuǎn)程查看電腦文件或者其他存儲設(shè)備》存在關(guān)聯(lián)。

2.用到的資源:

cybergarage.jar(這個做好久了酝静,具體地址找不到了节榜,自己搜搜看看吧)

3.核心代碼

PlayFileService.java,需要在主配置文件中注冊,并在應(yīng)用啟動或者需要轉(zhuǎn)換的時候開啟改服務(wù)宗苍。

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,需要在轉(zhuǎn)換服務(wù)中啟動該線程亿遂。

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();
    // 默認(rèn)的共享端口
    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();

        /**************************************************
         * 
         * 創(chuàng)建http服務(wù)器蛇数,接收共享請求
         * 
         *************************************************/
        // 重試次數(shù)
        int retryCnt = 0;
        // 獲取端口 2222
        int bindPort = getHTTPPort();

        HTTPServerList hsl = getHttpServerList();
        while (hsl.open(bindPort) == false) {
            retryCnt++;
            // 重試次數(shù)大于服務(wù)器重試次數(shù)時返回
            if (100 < retryCnt) {
                return;
            }
            setHTTPPort(bindPort + 1);
            bindPort = getHTTPPort();
        }
        // 給集合中的每個HTTPServer對象添加HTTPRequestListener對象
        hsl.addRequestListener(this);
        // 調(diào)用集合中所有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中是否包含參數(shù)
        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中定義了靜態(tài)變量ip="127.0.0.1",port=0,其他的就用到了根據(jù)文件名稱獲取MIME類型的方法,由于方法簡單且代碼太長就不貼了

4.開啟/關(guān)閉轉(zhuǎn)換服務(wù)

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

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

其他相關(guān)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末霞丧,一起剝皮案震驚了整個濱河市冕香,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌突那,老刑警劉巖构眯,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異惫霸,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)饵骨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進(jìn)店門居触,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妖混,“玉大人轮洋,你說我怎么就攤上這事∠殚梗” “怎么了汉柒?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長兽间。 經(jīng)常有香客問我正塌,道長嘀略,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任帜羊,我火速辦了婚禮鸠天,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘窥淆。我一直安慰自己巍杈,他們只是感情好扛伍,可當(dāng)我...
    茶點故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鳖宾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鼎文。 梳的紋絲不亂的頭發(fā)上因俐,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天,我揣著相機(jī)與錄音蓉坎,去河邊找鬼胡嘿。 笑死,一個胖子當(dāng)著我的面吹牛衷敌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播助琐,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼瞒爬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了矢空?” 一聲冷哼從身側(cè)響起禀横,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤弥激,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后垃你,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體缭嫡,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡妇蛀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年笤成,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纵诞。...
    茶點故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡培遵,死狀恐怖登刺,靈堂內(nèi)的尸體忽然破棺而出茁裙,到底是詐尸還是另有隱情,我是刑警寧澤掉蔬,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布矾瘾,位于F島的核電站,受9級特大地震影響蛉迹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜北救,卻給世界環(huán)境...
    茶點故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一芜抒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧攘宙,春花似錦拐迁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至割卖,卻和暖如春患雏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背丙挽。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留平窘,地道東北人凳怨。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓肤舞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親李剖。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,654評論 2 354

推薦閱讀更多精彩內(nèi)容