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

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

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

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

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

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

3:視頻分頁(yè)展示在頁(yè)面上。

4:視頻簡(jiǎn)介

5:視頻評(píng)論

6:發(fā)表評(píng)論

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

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

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

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

那么我們下面就來(lái)簡(jiǎ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()方法會(huì)返回文件的完整名稱(chēng),即包括路徑
            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";
    }

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

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

頁(yè)面展示:

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

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

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

    springboot如何配置多kafka

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

    黄浦区| 临汾市| 涞源县| 郎溪县| 逊克县| 自治县| 太保市| 离岛区| 苏尼特左旗| 潜山县| 高尔夫| 万安县| 贡山| 察雅县| 洛隆县| 梅州市| 江口县| 米林县| 黔江区| 金川县| 柯坪县| 洪湖市| 麻城市| 河池市| 保定市| 余姚市| 华安县| 盖州市| 蒙阴县| 宜兴市| 波密县| 英吉沙县| 长丰县| 宝清县| 麟游县| 赫章县| 南木林县| 太保市| 芮城县| 中超| 五台县|