需要每次都重启Tomcat以使Xuggler正常工作

时间:2012-10-11 08:20:20

标签: java-ee tomcat xuggler

我正在尝试开发一个视频共享网站,用户可以在其中上传视频,一旦上传视频,就会为该视频创建缩略图。这是用Xuggler完成的。这工作正常,但是当我第二次或下次这样做时,它不会生成图像。知道为什么吗?视频将在下次上传,但不会为其生成图像。当Web应用程序第一次运行,并且用户上传视频时,仅在该时间生成图像。因此,为了为每次上传生成图像,我必须重新启动我的服务器(Tomcat)。这是什么治疗方法???我在下面列出我的代码,我希望我会得到很好的回应。

此代码来自 index.jsp ,我的网络应用程序的第一页,用户将从此页面上传视频:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <script type="text/javascript">
        function validate(){
            document.getElementById("mybutton").style.visibility="visible";
            document.getElementById("file_browse").style.visibility="hidden";

        }
    </script>
    <title>ADMIN PAGE</title>
    <style type="text/css">
        #Text1
        {
            width: 303px;
            height: 28px;
            margin-top: 0px;
        }
        #Button1
        {
            width: 84px;
            margin-bottom: 4px;
            height: 27px;
        }
        #Button2
        {
            width: 100px;
            height: 35px;
        }
        #Button3
        {
            width: 100px;
            height: 35px;
        }
        .style1
        {
            font-size: xx-large;
        }
        .style2
        {
            color: #CC0000;
            word-spacing: inherit;
        }
        #Button4
        {
            width: 113px;
            height: 32px;
        }
        #Button5
        {
            width: 114px;
            height: 32px;
        }
        .newStyle1
        {
            font-style: oblique;
        }
        .style3
        {
            font-style: oblique;
            color: #FF0000;
            font-size: xx-large;
        }
    </style>
   </head>
   <body bgcolor="#E7EEE7">
    <form id="form1" runat="server">

        <div style="border-style: groove; border-width: thin;height: 77px; width:    
   999px; margin-right: 1px; margin-top: 0px">

            &nbsp;&nbsp;<span class="style1"></span> <span   
   class="style3">YouTUBE</span></span><span
                class="style2">&nbsp;  
   </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input id="Text1" type="text" /><input id="Button1" type="button"
                                                   value="Search"/>

               <input id="Button2" type="button" value="Admin"/>
            <input id="Button3" type="button" value="Logout" /></div>
    </form>
    <div style="border-style: groove; border-width: thin;width: 155px; height: 906px;    
    margin-top:0px;">
    </div>
    <div style="border-style: groove; border-width: thin;height: 908px; width: 250px;   
    margin-left: 750px; margin-top: -907px; margin-bottom: 0px; margin-right: 0px;">
    </div>
    <div style="border-style: groove; border-width: thin; width: 586px; height: 487px;  
    margin-left: 159px; margin-top: -910px; margin-right: 7px;">


        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="600"   
    height="344">

            <object type="application/x-shockwave-flash" data="player.swf" width="580"   
    height="444">

                <param name="movie" value="Justin King Lesson 2.flv" />
                <param name="wmode" value="transparent" />
                <param name="bgcolor" value="#FFFFFF" />
                <param name="quality" value="high" />
                <param name="allowFullScreen" value="true" />
                <param name="allowscriptaccess" value="always" />
                <a href="http://www.adobe.com/go/getflash">
                <img     
     src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" 
     alt="Get Adobe Flash player" />
                </a>
                <param name="flashvars" value="vdo=Justin King Lesson  
     2.flv&amp;autoplay=false" />


            </object>

        </object>
    </div>
    <p>

        <input id="Button4" type="button" value="Upload"/>
        <input id="Button5" type="button" value="Download" /></p>

    <form ENCTYPE="multipart/form-data" ACTION=
          "VT" METHOD="POST">

        <table style="margin-top:30px;margin-left:160px;">
            <tr>
                <td colspan="1"><p align="center"/></td>
                <td>
                </td>
                <td><div id='file_browse_wrapper'>
                        <input type="file" name="F1" id="file_browse"   
    onclick="validate()" /></div></td>

            </tr>
        </table>
        <table style="margin-top:-37px;margin-left:164px;">

            <div id="mybutton_wrapper">
                <tr><td><p align="right"><input TYPE="submit" VALUE="" id="mybutton"  
    style="visibility:hidden"/></p></td></tr>
            </div>



            <tr><td colspan="2"></td></tr>

        </table>

    </form>
    </body>
    </html>

通过执行 VT.java servlet进行上传:

package xugglerPack;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.Image;


public class VT extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    String user = "";

    String contentType = request.getContentType();
    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
        DataInputStream in = new DataInputStream(request.getInputStream());
        int formDataLength = request.getContentLength();
        byte dataBytes[] = new byte[formDataLength];
        int byteRead = 0;
        int totalBytesRead = 0;
        while (totalBytesRead < formDataLength) {
            byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
            totalBytesRead += byteRead;
        }
        String file = new String(dataBytes);

        String saveFile = file.substring(file.indexOf("filename=\"") + 10);
        saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
        saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,  
        saveFile.indexOf("\""));
        int lastIndex = contentType.lastIndexOf("=");
        String boundary = contentType.substring(lastIndex + 1, contentType.length());
        int pos;
        pos = file.indexOf("filename=\"");
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        int boundaryLocation = file.indexOf(boundary, pos) - 4;
        int startPos = ((file.substring(0, pos)).getBytes()).length;
        int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

        String rootPath = "C:\\Users\\JD\\Documents\\NetBeansProjects\\xuggler\\web";

        session.setAttribute("myFolderpath", rootPath);
        FileOutputStream fileOut = new FileOutputStream(rootPath + "\\" + saveFile);

        fileOut.write(dataBytes, startPos, (endPos - startPos));
        fileOut.flush();
        fileOut.close();
        String myFile = rootPath.concat("\\").concat(saveFile);
        Main main=new Main();
        main.main(saveFile,myFile);

        RequestDispatcher rd = request.getRequestDispatcher("findSong.jsp");
        rd.forward(request, response);
    }

   }

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
    processRequest(request, response);
  } 

   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
    processRequest(request, response);
  }

   @Override
   public String getServletInfo() {
   return "Short description";
    }

  }

在上传过程中,使用Xuggler从视频中使用 Main.java 生成图像:

  package xugglerPack;

  import java.awt.image.BufferedImage;

  import java.io.File;

  import java.io.IOException;

  import javax.imageio.ImageIO;

  import com.xuggle.mediatool.IMediaReader;

  import com.xuggle.mediatool.MediaListenerAdapter;

  import com.xuggle.mediatool.ToolFactory;

  import com.xuggle.mediatool.event.IVideoPictureEvent;

  import com.xuggle.xuggler.Global;

  import java.util.Random;

    public class Main {

public static final long SECONDS_BETWEEN_FRAMES = 50;

private static final String outputFilePrefix =  
"C:\\Users\\JD\\Documents\\NetBeansProjects\\xuggler\\web\\";
// The video stream index, used to ensure we display frames from one and
// only one video stream from the media container.
private static long mVideoStreamIndex = SECONDS_BETWEEN_FRAMES;
//public String saveFile  =  "";
// Time of last frame write
private static long mLastPtsWrite = Global.DEFAULT_PTS_PER_SECOND +  
SECONDS_BETWEEN_FRAMES;
public static final long MICRO_SECONDS_BETWEEN_FRAMES =
        (long) (Global.DEFAULT_PTS_PER_SECOND * SECONDS_BETWEEN_FRAMES);

public void main(String saveFile, String inputFilename) {



    IMediaReader mediaReader = ToolFactory.makeReader(inputFilename);



    // stipulate that we want BufferedImages created in BGR 24bit color space

    mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);



    mediaReader.addListener(new ImageSnapListener());



    // read out the contents of the media file and

    // dispatch events to the attached listener

    while (mediaReader.readPacket() == null);



}

private static class ImageSnapListener extends MediaListenerAdapter {

    public void onVideoPicture(IVideoPictureEvent event) {



        if (event.getStreamIndex() != SECONDS_BETWEEN_FRAMES) {

            // if the selected video stream id is not yet set, go ahead an

            // select this lucky video stream

            if (mVideoStreamIndex == SECONDS_BETWEEN_FRAMES) {
                mVideoStreamIndex = event.getStreamIndex();
            } // no need to show frames from this video stream
            else {
                return;
            }

        }



        // if uninitialized, back date mLastPtsWrite to get the very first frame



        if (mLastPtsWrite == Global.DEFAULT_PTS_PER_SECOND + SECONDS_BETWEEN_FRAMES) {
            mLastPtsWrite = event.getTimeStamp() + SECONDS_BETWEEN_FRAMES;
            // String outputFilename = dumpImageToFile(event.getImage());
            dumpImageToFile(event.getImage());
        }



        // if it's time to write the next frame


        /*      if (event.getTimeStamp() - mLastPtsWrite
        >= MICRO_SECONDS_BETWEEN_FRAMES) {


        String outputFilename = dumpImageToFile(event.getImage());

        // indicate file written

        double seconds = ((double) event.getTimeStamp())
        / Global.DEFAULT_PTS_PER_SECOND;

        System.out.printf(
        "at elapsed time of %6.3f seconds wrote: %s\n",
        seconds, outputFilename);


        // update last write time

        mLastPtsWrite += MICRO_SECONDS_BETWEEN_FRAMES;

        } */


    }

    private String dumpImageToFile(BufferedImage image) {


        try {
            Random rg = new Random();
            int random = rg.nextInt(500);
            //   long randomlong = rg.nextLong();

            String outputFilepath = outputFilePrefix
                    + random + ".png";
            String outputFilename = random + ".png";
            String parts[] = outputFilename.split("\\.");
            String part1 = parts[0];
            System.out.println("the filename is splitted into two , where the name of  
     the file is " + part1);

            ImageIO.write(image, "png", new File(outputFilepath));

            DBHandler db = new DBHandler();
            db.insertpic(outputFilename, part1, "me", outputFilepath);

            return outputFilename;

        } catch (IOException e) {

            e.printStackTrace();

            return null;

        }

      }
   }
}

我之前说过的问题是,当我运行我的应用程序时,视频会上传并且图像会在那时生成,但下次上传视频时,只会上传视频而不是图片。我必须重新启动Tomcat再次实现相同,所以请帮忙。在此先感谢并抱歉让这个话题成为一个很长的话题。

1 个答案:

答案 0 :(得分:0)

尝试在Main类中删除这两个变量的static关键字:

private static long mVideoStreamIndex = SECONDS_BETWEEN_FRAMES;
private static long mLastPtsWrite = Global.DEFAULT_PTS_PER_SECOND + SECONDS_BETWEEN_FRAMES;

如果你让它们保持静态,它们的价值将会保持不变,我怀疑这是你想要的。如果我错了,请纠正我。