java 项目开发 实现上传视频。

具体我也不知道怎么说,希望那那位朋友能指点一二。
2024-11-03 02:19:27
推荐回答(3个)
回答1:

上传视频?只是上传的话,相当于,上传文件了
html如下









java如下
MultipartTestServlet.java

package com.bug.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

public class MultipartTestServlet extends HttpServlet {

public MultipartTestServlet() {
super();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("gbk");
RequestContext requestContext = new ServletRequestContext(request);

if(FileUpload.isMultipartContent(requestContext)){

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File("c:/tmp/"));
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setHeaderEncoding("gbk");
upload.setSizeMax(2000000);
List items = new ArrayList();
try {
items = upload.parseRequest(request);
} catch (FileUploadException e1) {
System.out.println("文件上传发生错误" + e1.getMessage());
}

Iterator it = items.iterator();
while(it.hasNext()){
FileItem fileItem = (FileItem) it.next();
if(fileItem.isFormField()){
System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + new String(fileItem.getString().getBytes("iso8859-1"), "gbk"));
}else{
System.out.println(fileItem.getFieldName() + " " +
fileItem.getName() + " " +
fileItem.isInMemory() + " " +
fileItem.getContentType() + " " +
fileItem.getSize());

if(fileItem.getName()!=null && fileItem.getSize()!=0){
File fullFile = new File(fileItem.getName());
File newFile = new File("c:/temp/" + fullFile.getName());
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("文件没有选择 或 文件内容为空");
}
}

}
}
}

}
web.xml中加入


MultipartTestServlet
com.bug.servlet.MultipartTestServlet


需要的包到这三个地方下载
http://jakarta.apache.org/commons/fileupload/下载
http://jakarta.apache.org/commons/io/下载
在http://www.servlets.com/cos/下载

关于视频的播放,你只能自己再去找了

回答2:

上传下载,是web项目基本的功能,至于播放不是我们讨论的问题,使用commons-fileupload框架或者其他,原理是使用request获取文件流,使用i/o流读取上传文件,记得页面将from的enctype设置为"multipart/form-data",使用file文件域上传。

回答3:

上传视频就是上传文件撒, 我建议你用jquery 的upload .
再写一个action 吧传来的文件流保存到DB.