溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ssm怎么實現(xiàn)視頻的上傳和播放

發(fā)布時間:2022-03-01 10:44:07 來源:億速云 閱讀:192 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)ssm怎么實現(xià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對象并返回 通常用的實現(xiàn)類為: SimpleAuthenticationInfo
        //以下信息是從數(shù)據(jù)庫中獲取的.
        //(1). principal : 認(rèn)證的實體信息 可以是 username 也可以是數(shù)據(jù)表對應(yīng)的用戶的實體類對象
        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作為鹽值 因為用戶名是唯一的
        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;
    }

關(guān)于“ssm怎么實現(xiàn)視頻的上傳和播放”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ssm
AI