ユーザーがビデオをアップロードできるビデオ共有 Web サイトを開発しようとしています。ビデオがアップロードされるとすぐに、そのビデオのサムネイル画像が作成されます。これは Xuggler で行われます。これはうまくいきますが、2回目または次回に行うと、画像が生成されません。理由はありますか?動画は次回アップロードされますが、画像は生成されません。Web アプリケーションが初めて実行され、ユーザーが動画をアップロードすると、そのときだけ画像が生成されます。したがって、アップロードごとに画像を生成するには、サーバー (Tomcat) を再起動する必要があります。これの治療法は何ですか?以下にコードをリストしています。良い反応が得られることを願っています。
このコードは 、私の Web アプリケーションの最初のページである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">
<span class="style1"></span> <span
class="style3">YouTUBE</span></span><span
class="style2">
</span>
<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&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サーブレットの実行によって行われます。
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を再起動する必要があるので、助けてください。事前に感謝し、これを長いトピックにして申し訳ありません.