Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upload and play videos with ssm

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to upload and play videos on ssm. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Video upload: @ 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 (); / / parse data media.setName (request.getParameter ("name")) Media.setDescription (request.getParameter ("description")); boolean flag = false; / / Mark whether transcoding is successful or not / / upload the file ServletContext sctx = request.getServletContext (); / / get the path to save the file String basePath = sctx.getRealPath ("videos") / / get the file name String fileUrl = file.getOriginalFilename (); / / on some operating systems, the item.getName () method returns the full name of the file, including the path String fileType = fileUrl.substring (fileUrl.lastIndexOf (".")) / / intercept file format / / generate file name String serialName = String.valueOf (System.currentTimeMillis ()) by custom method; / / File to be transcoded File uploadFile = new File (basePath + "/ temp/" + serialName + fileType); / / Save file Streams.copy (file.getInputStream (), new FileOutputStream (uploadFile.getAbsolutePath ()), true) / / determine the file size if (file.getSize () > 500 * 1024 * 1024) {message = "upload failed! The file you uploaded is too large, and the system allows a maximum of 500m ";} String codcFilePath = basePath +" / "+ serialName +" .flv "; / / set the save path of the file converted to flv format String mediaPicPath = basePath +" / images "+ File.separator + serialName +" .jpg " / / set the save path for uploading video screenshots / / get the storage path of the configured conversion tool (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 (); / / Transcoding flag = serviceFactory.getMediaService () .executeCodecs (ffmpegPath, uploadFile.getAbsolutePath (), codcFilePath, mediaPicPath) If (flag) {/ / transcoding successfully. Add the video information serviceFactory.getMediaService (). SaveMedia (media) to the data table; message= "uploaded successfully";} request.setAttribute ("message", message);} catch (Exception e) {e.printStackTrace () } MyWebPrinter.print (response, "alert ('" + message+ "'); _ window.location.href='indexing.cphtml';");} Video playback: @ 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" }

Users log in using the shiro security framework:

Public class ShiroRealm extends AuthorizingRealm {@ Autowired UserService userService; protected AuthenticationInfo doGetAuthenticationInfo (AuthenticationToken authenticationToken) throws AuthenticationException {/ / the authenticationToken here is the same as the UsernamePasswordToken in controller, which is the / / System.out.println ("doGetAuthenticationInfo" + authenticationToken.hashCode ()) passed from controller; / / 1. Convert AuthenticationToken to UsernamePasswordToken UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken; / / 2. Get username String username = upToken.getUsername (); / / 3 from UsernamePasswordToken. Call the database method to query the user record (login name and password) / / System.out.println corresponding to username from the database ("get the user information corresponding to username:" + username + "from the database."); User user = userService.findUserByEmail (username); System.out.println (user.getEmail () + "," + user.getPassword ()); / / 4. If the user does not exist, you can throw a UnknownAccountException exception / / if ("unknown" .equals (username)) {/ / throw new UnknownAccountException ("user does not exist!"); / /} / / 5. Based on the user information, decide whether or not to throw other AuthenticationException exceptions / / if ("monster" .equals (username)) {/ / throw new LockedAccountException ("user is locked"); / /} / / 6. According to the situation of the user to build the AuthenticationInfo object and return the commonly used implementation class is: SimpleAuthenticationInfo / the following information is obtained from the database. / (1). Principal: the authenticated entity information can be username or the entity class object Object principal = username; / / (2) of the user corresponding to the data table. Credentials: password. Object credentials = null; if (user.getEmail (). Equals (username)) {credentials = user.getPassword ();} / / (3). RealmName: the name of the current realm object calls the getName () method of the parent class to String realmName = getName (); / / (4). Salt: salt value here username is used as the salt value because the user name is unique ByteSource salt = ByteSource.Util.bytes (username); SimpleAuthenticationInfo info = null; info = new SimpleAuthenticationInfo (principal,credentials,salt,realmName); return info;} / / the encrypted password in this example is 123456, encrypted twice 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; / / encrypt a string Object result = new SimpleHash (hashAlgorithmName, credentials, salt, hashIterations); System.out.println (result) } / / Authorization will be called back by the shiro method protected AuthorizationInfo doGetAuthorizationInfo (PrincipalCollection principalCollection) {/ / 1. Obtain the information of the logged-in user from PrincipalCollection / / Note that if it is multiple realm, the acquired principal is also Object principal = principalCollection.getPrimaryPrincipal (); / / 2. Use the information of the logged-in user to check the current user's role or permissions (you may need to query the database) User_Role user_role = userService.findUserRoleByEmail ((String) principal); System.out.println ("role is:" + user_role.getRole_name ()); Set roles = new HashSet (); roles.add ("user") / / add user permission if (user_role.getRole_name (). Equals ("admin")) {roles.add (user_role.getRole_name ()) to all users; / / if the user's role is admin, add another admin permission} / / 3. Create a SimpleAuthorizationInfo and set its roles property. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo (roles) / / 4. Returns the SimpleAuthorizationInfo object. Return info;} this is the end of the article on "how to upload and play videos on ssm". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report