---
title: "JSF upload& play vedio"
author: "dragon0929"
date: "2010-06-10T14:43:46Z"
tags: []
categories: ["编程语言"]
slug: "jsf-upload-play-vedio"
draft: false
lastmod: "2010-06-10T14:43:46Z"
id: 688848
url: "https://dragon0929.iteye.com/blog/688848"
---

JSF Upload Vedio

 

**jsp:**

 

&lt;%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %&gt;  
&lt;%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %&gt;

&lt;%@ taglib prefix="rich" uri="http://richfaces.ajax4jsf.org/rich" %&gt;  
&lt;%@ taglib prefix="a4j" uri="http://richfaces.org/a4j" %&gt;

&lt;h:panelGroup&gt;  
&lt;script type="text/javascript" src="&lt;h:outputText value="#{request.contextPath}/js/flowplayer.3.1.2.min.js"/&gt;"&gt;&lt;/script&gt;  
    &lt;script type="text/javascript"&gt;  
        jQuery(function() {  
           showPlayer();  
        });

        function showPlayer() {  
            $f("player", "&lt;h:outputText value="#{request.contextPath}/swf/flowplayer-3.1.2.swf"/&gt;", {  
                clip: {  
                    autoPlay: false,  
                    autoBuffering: true  
                }  
            });  
        }  
    &lt;/script&gt;

    &lt;style type="text/css"&gt;  
        #player {  
            height:340px;  
            width: 500px;  
        }

        .selected-video {  
            font-weight:bold;  
        }  
    &lt;/style&gt;

    &lt;table style="width:100%;height:500px;"&gt;  
        &lt;tr&gt;  
            &lt;td style="width:40%;"&gt;  
                &lt;rich:dataTable id="table" value="#{artistManageBean.profileVideos}" var="video"  
                                style="width:100%;text-align:left;"&gt;  
                    &lt;h:column&gt;  
                        &lt;h:panelGroup&gt;  
                            &lt;div id="video&lt;h:outputText value="#{video.id}"/&gt;"  
                                &lt;h:outputText value="class='selected-video'"  
                                    rendered="#{artistManageBean.selectedVideo != null and artistManageBean.selectedVideo.id == video.id}"/&gt; &gt;  
                                &lt;a4j:commandLink id="preview" action="#{artistManageBean.doPreviewVideo}"  
                                                 value="#{video.description != '' ? video.description : 'Unknown Video'}"  
                                                 reRender="artist:table,artist:videoPreview" oncomplete="showPlayer();"/&gt;&lt;br/&gt;  
                                &lt;h:outputText value="#{video.preparedDuration.inMinutes} mins"  
                                              rendered="#{video.preparedDuration.inMinutes &gt; 1}"/&gt;  
                                &lt;h:outputText value="#{video.preparedDuration.inMinutes} min"  
                                              rendered="#{video.preparedDuration.inMinutes == 1}"/&gt;&lt;br/&gt;  
                                &lt;h:outputText value="less than minute"  
                                              rendered="#{video.preparedDuration.inMinutes &lt; 1}"/&gt;

                                &lt;a4j:commandLink id="edit" value="Edit" immediate="true"  
                                                 reRender="artist:uploadVideo"  
                                                 action="#{artistManageBean.doEditVideo}"/&gt;&nbsp;&nbsp;  
                                &lt;a4j:commandLink id="del" value="Delete" immediate="true"  
                                                 reRender="artist:table,artist:videoPreview"  oncomplete="showPlayer();"  
                                                 action="#{artistManageBean.doRemoveVideo}"/&gt;  
                            &lt;/div&gt;  
                        &lt;/h:panelGroup&gt;  
                    &lt;/h:column&gt;  
                &lt;/rich:dataTable&gt;  
                &lt;a4j:outputPanel id="uploadVideo"&gt;  
                      &lt;a4j:commandButton reRender="uploadVideo" rendered="#{artistManageBean.profileVideo == null}"  
                            value="Upload Video" action="#{artistManageBean.doCreateVideo}"/&gt;  
                    &lt;h:panelGrid  columns="2" rendered="#{artistManageBean.profileVideo != null}"&gt;  
                         &lt;h:outputText value="#{artistManageBean.profileVideo.name != null ? 'Replace Current Video' : 'Upload Video'}"/&gt;  
                         &lt;h:panelGroup id="video"&gt;  
                                   &lt;rich:fileUpload id="videoUpload" acceptedTypes="mpg,flv,mov,avi,mpeg,mp4,mkv" required="true"  
                                            immediate="true" listWidth="280" listHeight="60" autoclear="false"  
                                            maxFilesQuantity="1" addControlLabel="Upload" immediateUpload="true"  
                                            requiredMessage="Video is required."  
                                            fileUploadListener="#{artistManageBean.uploadVideoListener}"&gt;  
                                   &lt;/rich:fileUpload&gt;  
                               &lt;rich:message for="videoUpload" errorClass="error\_single"/&gt;  
                           &lt;/h:panelGroup&gt;  
                          
                        &lt;h:outputLabel value="Description: " /&gt;  
                        &lt;h:inputText value="#{artistManageBean.profileVideo.description}" /&gt;  
                        &lt;h:outputLabel value="Duration: " /&gt;  
                        &lt;h:selectOneListbox id="duration" style="width:50px;" size="1"  
                                        value="#{artistManageBean.profileVideo.duration}"&gt;  
                                    &lt;f:selectItem itemValue="1" itemLabel="1"/&gt;  
                                    &lt;f:selectItem itemValue="2" itemLabel="2"/&gt;  
                                    &lt;f:selectItem itemValue="3" itemLabel="3"/&gt;  
                                    &lt;f:selectItem itemValue="4" itemLabel="4"/&gt;  
                                    &lt;f:selectItem itemValue="5" itemLabel="5"/&gt;  
                        &lt;/h:selectOneListbox&gt;  
                           &lt;h:outputLabel value=" " /&gt;  
                        &lt;h:panelGroup&gt;  
                           &lt;a4j:commandButton id="upload" value="Upload" reRender="artist:table,artist:videoPreview,artist:uploadVideo"  
                                   oncomplete="showPlayer();" action="#{artistManageBean.doSaveVideo}"/&gt;  
                           &nbsp;&nbsp;  
                           &lt;a4j:commandButton id="cancelUpload" value="Cancel" reRender="artist:table,artist:uploadVideo"  
                                   action="#{artistManageBean.doCancelVideo}"/&gt;  
                           &lt;/h:panelGroup&gt;  
                    &lt;/h:panelGrid&gt;  
                   &lt;/a4j:outputPanel&gt;  
            &lt;/td&gt;  
            &lt;td style="vertical-align:top; width:60%;text-align:center;"&gt;  
                &lt;div id="preview" style="width:500px; height:400px;"&gt;  
                    &lt;a4j:outputPanel id="videoPreview"&gt;  
                        &lt;h:panelGroup rendered="#{artistManageBean.selectedVideo == null}"&gt;  
                            &lt;a id="player" href="#"&gt;&lt;/a&gt;  
                        &lt;/h:panelGroup&gt;  
                        &lt;h:panelGroup rendered="#{artistManageBean.selectedVideo != null}"&gt;  
                            &lt;h3&gt;&lt;h:outputText value="#{artistManageBean.selectedVideo.description != ''  
                                    ? artistManageBean.selectedVideo.description : 'Unknown Video'}"/&gt;&lt;/h3&gt;  
                            &lt;div style="width:100%;"&gt;  
                                &lt;a id="player" href="&lt;h:outputText value='#{request.contextPath}/video/#{artistManageBean.selectedVideo.name.fullPath}'/&gt;"&gt;&lt;/a&gt;  
                                &lt;h:panelGroup rendered="#{permissions.publicVideoGranted}"&gt;  
                                    &lt;h:outputLink id="viewVd" value="#{request.contextPath}/admin/video/#{artistManageBean.publicVideo.id}"  
                                                  rendered="#{artistManageBean.publicVideo != null}"  
                                                  title="Go to the public video"&gt;  
                                        &lt;h:outputText value="View public video"/&gt;  
                                    &lt;/h:outputLink&gt;  
                                    &lt;a4j:commandLink id="creatVd" value="Publicate video to website"  
                                                     action="#{artistManageBean.doPublishVideo}"  
                                                     rendered="#{artistManageBean.publicVideo == null}"  
                                                     oncomplete="showPlayer();"  
                                                     reRender="artist:videoPreview"/&gt;  
                                &lt;/h:panelGroup&gt;  
                            &lt;/div&gt;  
                        &lt;/h:panelGroup&gt;  
                    &lt;/a4j:outputPanel&gt;  
                &lt;/div&gt;  
            &lt;/td&gt;  
        &lt;/tr&gt;  
        
    &lt;/table&gt;  
     
     
&lt;/h:panelGroup&gt;

 

**Background bean:**

 

  private MediaVO&lt;ProfileVideo&gt; profileVideoVO = null;  
  private ProfileVideo selectedProfileVideo = null;

  private ProfileVideo profileVideo;  
  private boolean isEditVideo = false;

 

 public List&lt;ProfileVideo&gt; getProfileVideos() {  
        if (profileVideoVO == null) {  
            profileVideoVO = new MediaVO&lt;ProfileVideo&gt;();  
            if (getMemberProfile().getId() != null) {  
                try {  
                    profileVideoVO.getCurrent().addAll(profileVideoManager.getByMemberProfileId(  
                            getMemberProfile().getId()));  
                    if (profileVideoVO.hasCurrent()) {  
                        setSelectedVideo(CollectionUtils.first(profileVideoVO.getCurrent()));  
                    } else {  
                        profileVideo = new ProfileVideo();  
                    }  
                } catch (FinderException e) {  
                    e.printStackTrace();  
                }  
            } else {  
                selectedProfileVideo = null;  
            }  
        }  
        return profileVideoVO.getCurrent();  
    }

    public ProfileVideo getSelectedVideo() {  
        return selectedProfileVideo;  
    }

 

public String doPreviewVideo() {  
        ProfileVideo video = (ProfileVideo) requestMap().get("video");  
        if (video != null) {  
            setSelectedVideo(video);  
        }  
        else {  
            MessageUtil.addGlobalErrorMessage("Can't preview selected video: it's not found.");  
        }  
        return ActionForward.NONE;  
    }

 

 public String doRemoveVideo() {  
        ProfileVideo video = (ProfileVideo) requestMap().get("video");  
        if (video != null) {  
            profileVideoVO.addRemoved(video);

            MessageUtil.addGlobalInfoMessage("Video will be removed with saving artist profile.");  
        }  
        else {  
            MessageUtil.addGlobalErrorMessage("Can't remove video: it's not found.");  
        }  
        return ActionForward.NONE;  
    }

 

 public void uploadVideoListener(UploadEvent event) {  
        InputStream fileInputStream = null;  
        try {  
            final UploadItem upload = event.getUploadItem();  
            if (upload.isTempFile()) {  
                // remove previously removed video  
                removeOldVideo();  
                // upload new video  
                fileInputStream = new FileInputStream(upload.getFile());

                final VideoHelperWrapper wrapper = new VideoHelperWrapper();  
                wrapper.initialize();  
                wrapper.download(upload.getFileName(), fileInputStream);  
                profileVideo.setName(wrapper.getMediaName());  
             
                MessageUtil.addGlobalInfoMessage("Video was uploaded successfully.");  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
            MessageUtil.addGlobalErrorMessage("Cannot upload video.");  
        } finally {  
            DataAccessUtil.closeStream(fileInputStream);  
        }  
    }  
       
    public String doSaveVideo() {  
        if (profileVideo != null) {  
            final MediaName videoName = profileVideo.getName();  
            if (videoName == null) {  
                MessageUtil.addGlobalErrorMessage("Please, upload video file.");  
            } else {  
                ProfileVideo temp = new ProfileVideo();  
                temp.setName(videoName);  
                temp.setDescription(profileVideo.getDescription());  
                temp.setDuration(profileVideo.getDuration());  
                temp.setMemberProfileId(memberProfile.getId());  
                setSelectedVideo(temp);  
                if (profileVideo.getId() != null) {  
                    temp.setId(profileVideo.getId());  
                    profileVideoVO.addChanged(temp);  
                } else if (!isEditVideo){  
                    profileVideoVO.addCreated(temp);  
                }  
                profileVideo = null;  
            }  
        }  
        isEditVideo = false;  
        return ActionForward.NONE;  
    }  
     
    public String doCancelVideo() {  
         
        if (!isEditVideo && profileVideo.getName() != null) {  
            VideoHelper.removeFile(profileVideo.getName().getFullPath());  
        }  
        profileVideo = null;  
        isEditVideo = false;  
        return ActionForward.NONE;  
    }  
     
    public String doCreateVideo() {  
        profileVideo = new ProfileVideo();  
        isEditVideo= false;  
        return ActionForward.NONE;  
    }

    public String doEditVideo() {  
        isEditVideo = true;  
        ProfileVideo video = (ProfileVideo) requestMap().get("video");  
         
        if (video!= null) {  
            profileVideo = video;  
        } else {  
            profileVideo = new ProfileVideo();  
        }  
        return ActionForward.NONE;  
    }

    private void removeOldVideo() {  
        if (profileVideo.getName() != null) {  
            VideoHelper.removeFile(profileVideo.getName().getFullPath());  
            profileVideo.setName(null);  
        }  
    }

    public void setProfileVideo(ProfileVideo profileVideo) {  
        this.profileVideo = profileVideo;  
    }  
     
    public ProfileVideo getProfileVideo() {  
        return this.profileVideo;  
    }