最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ssm實(shí)現(xiàn)視頻的上傳與播放的示例代碼

 更新時間:2021年04月21日 09:36:13   作者:yuehailin  
這篇文章主要介紹了ssm實(shí)現(xiàn)視頻的上傳與播放的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

實(shí)現(xiàn)的功能:

1:實(shí)現(xiàn)視頻的上傳與播放。

2:使用shiro框架進(jìn)行登錄注冊。

3:視頻分頁展示在頁面上。

4:視頻簡介

5:視頻評論

6:發(fā)表評論

簡單介紹一下大概實(shí)現(xiàn)的思路:

首先主要的功能就是實(shí)現(xiàn)視頻的上傳與播放,那么我們就需要一個視頻上傳的界面,選擇視頻進(jìn)行上傳,那么上傳到哪兒呢?

這里我們有多重選擇,第一:我們可以將視頻轉(zhuǎn)換格式存在我們tomcat服務(wù)器里面,然后在數(shù)據(jù)庫里面存入tomcat中對應(yīng)的文件的路徑。第二:我們可以使用nginx來存儲我們的網(wǎng)頁的靜態(tài)資源。今天我就介紹上面一個簡單的。

對于視頻的簡介,評論,以及發(fā)表評論無非就是對數(shù)據(jù)庫中進(jìn)行增刪改查。

那么我們下面就來簡單的介紹一下核心代碼。

視頻上傳:

@RequestMapping(value = "dofunction", method = RequestMethod.POST)
    public void handler(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam("myvideo") MultipartFile file) throws IOException {
        String message = "";
        try {
            Video media = new Video();
            // 解析數(shù)據(jù)
            media.setName(request.getParameter("name"));
            media.setDescription(request.getParameter("description"));
            boolean flag = false; // 轉(zhuǎn)碼成功與否的標(biāo)記
            // 上傳文件
            ServletContext sctx = request.getServletContext();
            // 獲得保存文件的路徑
            String basePath = sctx.getRealPath("videos");
            // 獲得文件名
            String fileUrl = file.getOriginalFilename();
            // 在某些操作系統(tǒng)上,item.getName()方法會返回文件的完整名稱,即包括路徑
            String fileType = fileUrl.substring(fileUrl.lastIndexOf(".")); // 截取文件格式
            // 自定義方式產(chǎn)生文件名
            String serialName = String.valueOf(System.currentTimeMillis());
            // 待轉(zhuǎn)碼的文件
            File uploadFile = new File(basePath + "/temp/" + serialName + fileType);
 
            // 保存文件
            Streams.copy(file.getInputStream(),new FileOutputStream(uploadFile.getAbsolutePath()),true);
            // 判斷文件的大小
            if (file.getSize() > 500 * 1024 * 1024) {
                message = "上傳失??!您上傳的文件太大,系統(tǒng)允許最大文件500M";
            }
            String codcFilePath = basePath + "/" + serialName + ".flv"; // 設(shè)置轉(zhuǎn)換為flv格式后文件的保存路徑
            String mediaPicPath = basePath + "/images" + File.separator + serialName + ".jpg"; // 設(shè)置上傳視頻截圖的保存路徑
 
            // 獲取配置的轉(zhuǎn)換工具(ffmpeg.exe)的存放路徑
            String ffmpegPath = request.getServletContext().getRealPath("/tools/ffmpeg.exe");
 
            media.setAddress("videos/" + serialName + ".flv");
            media.setPicture("videos/images/" + serialName + ".jpg");
            media.setUptime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())));
            // 轉(zhuǎn)碼
            flag = serviceFactory.getMediaService().executeCodecs(ffmpegPath, uploadFile.getAbsolutePath(),
                    codcFilePath, mediaPicPath);
 
            if (flag) {
                // 轉(zhuǎn)碼成功,向數(shù)據(jù)表中添加該視頻信息
                serviceFactory.getMediaService().saveMedia(media);
                message="上傳成功";
            }
            request.setAttribute("message", message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        MyWebPrinter.print(response,"<script>alert('"+message+"');window.location.href='indexing.cphtml';</script>");
    }

視頻播放:

@RequestMapping("play")
    public String play(int id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idstr = id + "";
        int mediaId = -1;
       Video media = null;
        if (null != idstr) {
            mediaId = Integer.parseInt(idstr);
        }
        try {
            media = serviceFactory.getMediaService().queryMediaById(mediaId);
            System.out.println(media.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("media", media);
        return "video-detail";
    }

用戶使用shiro安全框架進(jìn)行登錄:

public class ShiroRealm extends AuthorizingRealm{
 
    @Autowired
    UserService userService;
 
    protected AuthenticationInfo doGetAuthenticationInfo
            (AuthenticationToken authenticationToken) throws AuthenticationException {
        //此處的authenticationToken和controller中的UsernamePasswordToken是同一個,是controller中傳過來的
        //System.out.println("doGetAuthenticationInfo " + authenticationToken.hashCode());
 
        //1. 把 AuthenticationToken 轉(zhuǎn)換為 UsernamePasswordToken
        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
 
        //2. 從 UsernamePasswordToken 中來獲取 username
        String username = upToken.getUsername();
 
        //3. 調(diào)用數(shù)據(jù)庫的方法, 從數(shù)據(jù)庫中查詢 username 對應(yīng)的用戶記錄(登錄名和密碼)
        //System.out.println("從數(shù)據(jù)庫中獲取 username: " + username + " 所對應(yīng)的用戶信息.");
        User user = userService.findUserByEmail(username);
        System.out.println(user.getEmail() + ", " + user.getPassword());
 
        //4. 若用戶不存在, 則可以拋出 UnknownAccountException 異常
//        if("unknown".equals(username)){
//            throw new UnknownAccountException("用戶不存在!");
//        }
 
        //5. 根據(jù)用戶信息的情況, 決定是否需要拋出其他的 AuthenticationException 異常
//        if("monster".equals(username)){
//            throw new LockedAccountException("用戶被鎖定");
//        }
 
        //6. 根據(jù)用戶的情況來構(gòu)建 AuthenticationInfo對象并返回 通常用的實(shí)現(xiàn)類為: SimpleAuthenticationInfo
        //以下信息是從數(shù)據(jù)庫中獲取的.
        //(1). principal : 認(rèn)證的實(shí)體信息 可以是 username 也可以是數(shù)據(jù)表對應(yīng)的用戶的實(shí)體類對象
        Object principal = username;
        //(2). credentials : 密碼.
        Object credentials = null;
        if(user.getEmail().equals(username)){
            credentials = user.getPassword();
        }
        //(3). realmName : 當(dāng)前realm對象的name 調(diào)用父類的getName()方法即可
        String realmName = getName();
        //(4). salt : 鹽值 這里用username作為鹽值 因?yàn)橛脩裘俏ㄒ坏?
        ByteSource salt = ByteSource.Util.bytes(username);
 
        SimpleAuthenticationInfo info = null;
        info = new SimpleAuthenticationInfo(principal,credentials,salt,realmName);
 
        return info;
    }
 
    //測試獲取加密后的密碼 本例原密碼123456,加密2次
    public static void main(String[] args) {
        String hashAlgorithmName = "MD5";
        Object credentials = "123456";
        //Object salt = ByteSource.Util.bytes("lewy@9.com");//9be0a8423bbe47b9ab62b964d0e5b434
        Object salt = ByteSource.Util.bytes("muller@25.com");//9c377556e3611b4e4fe3d844f1a7135a
        int hashIterations = 2;
 
        //將一個字符串進(jìn)行MD5加密
        Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
        System.out.println(result);
    }
 
    //授權(quán)會被shiro回調(diào)的方法
    protected AuthorizationInfo doGetAuthorizationInfo
               (PrincipalCollection principalCollection) {
        //1. 從 PrincipalCollection 中來獲取登錄用戶的信息
        //   注意如果是多realm,獲取的principal也是有順序的
        Object principal = principalCollection.getPrimaryPrincipal();
 
        //2. 利用登錄的用戶的信息來查用戶當(dāng)前用戶的角色或權(quán)限(可能需要查詢數(shù)據(jù)庫)
        User_Role user_role = userService.findUserRoleByEmail((String) principal);
        System.out.println("角色為:" + user_role.getRole_name());
 
        Set<String> roles = new HashSet<String>();
        roles.add("user");//給所有用戶添加user權(quán)限
        if(user_role.getRole_name().equals("admin")){
            roles.add(user_role.getRole_name());//如果用戶的角色是admin,再添加一個admin權(quán)限
        }
 
        //3. 創(chuàng)建 SimpleAuthorizationInfo, 并設(shè)置其 roles 屬性.
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
 
        //4. 返回 SimpleAuthorizationInfo 對象.
        return info;
    }

頁面展示:

實(shí)現(xiàn)的過程,controller調(diào)用service中的方法,將結(jié)果呈現(xiàn)給在jsp頁面上,然后service層調(diào)用dao層中的對數(shù)據(jù)庫操作的方法。

注:視頻上傳是將視頻上傳到tomcat服務(wù)器上面,當(dāng)服務(wù)器處于開著的狀態(tài)可以進(jìn)行視頻的播放,當(dāng)服務(wù)器
關(guān)掉之后視頻將清空,如果tomcat設(shè)置的是關(guān)閉之后,將清空tomcat中的緩存,就會出現(xiàn)在mysql數(shù)據(jù)庫中的存儲的視頻地址將失效,當(dāng)然如果上傳的視頻保存在項(xiàng)目根路徑下,那么就不會出現(xiàn)這種情況。

到此這篇關(guān)于ssm實(shí)現(xiàn)視頻的上傳與播放的示例代碼的文章就介紹到這了,更多相關(guān)ssm 視頻上傳播放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • kaptcha驗(yàn)證碼組件使用簡介解析

    kaptcha驗(yàn)證碼組件使用簡介解析

    這篇文章主要介紹了kaptcha驗(yàn)證碼組件使用簡介解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • java設(shè)計(jì)模式之工廠方法模式

    java設(shè)計(jì)模式之工廠方法模式

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之工廠方法模式,什么是java工廠方法模式,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 一文告訴你為什么要重寫hashCode()方法和equals()方法

    一文告訴你為什么要重寫hashCode()方法和equals()方法

    本篇文章帶大家了解一下為什么重寫hashCode()方法和equals()方法,文中有非常詳細(xì)的說明以及代碼示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • SpringBoot動態(tài)生成接口實(shí)現(xiàn)流程示例講解

    SpringBoot動態(tài)生成接口實(shí)現(xiàn)流程示例講解

    最近遇到一個需求,需要在程序運(yùn)行過程中,可以動態(tài)新增接口,自定義接口參數(shù)名稱,基本類型,以及請求方法,請求頭等等。通過幾天的研究,找到了我需要的解決方案
    2023-01-01
  • springboot如何配置多kafka

    springboot如何配置多kafka

    這篇文章主要介紹了springboot如何配置多kafka問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 最新評論

    沐川县| 永寿县| 定襄县| 子洲县| 崇仁县| 本溪| 虎林市| 西华县| 昭觉县| 裕民县| 乌拉特中旗| 克什克腾旗| 安岳县| 壶关县| 故城县| 绥中县| 和顺县| 客服| 金坛市| 福鼎市| 阜阳市| 静安区| 尤溪县| 化州市| 宁南县| 水城县| 黄冈市| 佳木斯市| 汉寿县| 万安县| 珠海市| 蛟河市| 循化| 策勒县| 清镇市| 固镇县| 文成县| 东宁县| 成都市| 海林市| 潼南县|