2012-10-11 72 views
0

我想开发一个视频共享网站,用户可以上传视频,并且一旦视频上传,就会为该视频创建一个缩略图图像。这是用Xuggler完成的。这工作正常,但是当我第二次或下次执行时,它不会生成图像。任何想法为什么?这些视频会在下次上传,但不会为其生成图像。当Web应用程序第一次运行,并且用户上传视频时,只有在该图像生成的时候。因此,为了生成每次上传的图像,我不得不重新启动我的服务器(Tomcat)。这是什么治疗?我在下面列出我的代码,我希望我会得到良好的回应。需要重新启动Tomcat每次让Xuggler工作

这个代码是从的index.jsp,我的Web应用程序的第一页时,用户会上传视频从这个页面:

<%@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"; 
    } 

    } 

在与Main.java使用许视频生成的图像上载ggler:

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才能再次实现,所以请帮助。提前致谢,并且很抱歉让这个话题变得很长。

+0

我假设你检查了Tomcat日志并没有发现任何错误? –

回答

0

尝试在主类这两个变量去除static关键字:

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

如果你让他们静其价值将被保留到以后的呼叫,我怀疑这真的是你想要的。如我错了请纠正我。

相关问题