页面:
上传文件时的关键词:Enctype =“ Multipart/form-data”
<%@ page Lagansy =“ java” import =“ java.util。 字符串basepath = request.getScheme()+“://”+request.getServername()+“:”+request.getServerport() nofollow“> <title> </title> <meta http-equiv =“ pragma” content =“ no-cache”> <meta http-equiv =“ cache-control” content =“ no-cache”> <meta http-equiv =“ content =“关键字1,关键字2,键word3”> <meta http-equiv =“ descript Enctype =“ Multipart/form-data”> <div align =“ center”> <div> <div> <input type =“ file” name =“ file” id =“ file”> <br> <br> <br> <br> <br> <br>
后台:
控制器
导入javax.servlet.http.httpservletrequest; import model.fileentity; import org.springframework.stringframework.stereotype.controller; import org.springframework.ui.modelmap; import org org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.servlet.modelandview;@controller@controller@requestmapping(“/uploadflv”)public class class class uploadController {@requestmapping(value =“/upload”,method =“/upload. MultipartFile MultipartFile,httpservletrequest request,modelmap映射){字符串消息=“”; fileentity entity = new fileentity(); fileUploadTool fileuploadtool = new FileUploadTool();尝试{entity = fileuploadtool.createfile(MultipartFile,request); if(entity!= null){// service.savefile(entity);消息=“上传成功”; map.put(“实体”,实体); map.put(“结果”,消息); } else {message =“上传失败”; map.put(“结果”,消息); }} catch(异常e){e.printstacktrace(); }返回新的modelAndView(“结果”,地图); }}}工具类
import java.io.File;import java.io.IOException;import java.sql.Timestamp;import java.text.DecimalFormat;import java.util.Arrays;import java.util.Iterator;import javax.servlet.http.HttpServletRequest;import model.FileEntity;import org.springframework.web.multipart.multipartfile; public class fileUploadTool {transfmediatool transfmediatool = new transfmediatool(); //文件最大500m私有静态long upload_maxsize = 800 * 1024 * 1024; //文件允许格式私有静态字符串[]允许files = {“ .rar”,“ .doc”,.docx“,” .zip“,” .pdf“,” .txt“ .swf”,“ .xlsx”,“ .gif”,“ .gif”,“ .gif”,“ .png”,.png“ .png”,“ .jpg”,“ .jpg”,“ .jpeg”,“ .jpeg”,“”,“”。 “ .flv”,“ .ppt”,“ .avi”,“ .mpg”,“ .wmv”,“ .3GP”,“ .MOV”,“ .ASF”,“ .ASX”,“ .VOB”,“ .WMV9”,“ .WMV9”,“ .RM”,“ .RM”,“ .RMVB”}}; // 允许转码的视频格式( ffmpeg)私有静态字符串[] lassionflv = {“ .avi”,“ .mpg”,“ .wmv”,“ .3GP”,“ .MOV”,“ .MOV”,“ .ASF”,“ .ASX”,“ .VOB”}; //允许的视频转码格式(mencoder)private static String [] allowavi = {“ .wmv9”,“ .rm”,“ .rmvb”}; public fileentity createFile(MultipartFile MultipartFile,httpservletrequest request){fileentity entity = new fileNentity(); boolean bflag = false;字符串fileName = multipartFile.getOriginalFileName()。toString(); //判断文件不为空if(multipartFile.getSize()!= 0 &&!promptratfile.isempty()){bflag = true; //判断文件大小if(multipartfile.getSize()<= upload_maxsize){bflag = true; //文件类型判断If(this.checkfiletype(filename)){bflag = true; } else {bflag = false; system.out.println(“文件类型不允许”); }} else {bflag = false; system.out.println(“文件大小超范围”); }} else {bflag = false; system.out.println(“文件为空”); } if(bflag){字符串logopathdir =“/video/”;字符串logorealpathdir = request.getSession()。getServletContext()。getRealPath(logopathdir); //上传到本地磁盘//字符串logorealpathdir =“ e:/upload”;文件logosavefile =新文件(logorealpathdir); if(!logoSaveFile.exists()){logosavefile.mkdirs(); }字符串名称= filename.substring(0,filename.lastIndexof(“。”)); system.out.println(“文件名称:” +名称); //新的文件名字符串newfileName = this.getName(filename); //文件扩展名字符串fileend = this.getFileext(filename); //绝对路径字符串filenamedirs = logorealpathdir + file.separator + newfilename + fileend; system.out.println(“:” + filenamedirs);文件filedirs =新文件(fileNamedirs); //转入文件尝试{multipartfile.transferto(filedirs); } catch(illegalstateException e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); } //相对路径entity.setType(fileEnd);字符串filedir = logopathdir + newfileName + fileend; StringBuilder Builder = new StringBuilder(FileDir);字符串FinalFileDir = Builder.Substring(1); //大小存储为字符串大小= this.getSize(filedirs); //源文件保存路径字符串avipath = filedirs.getabsolutepath(); //转码avi // boolean flag = false; if(this.checkavitype(fileEnd)){// a avi codcavipath = logorealpathdir + file.separator + newfilename +“ .avi”; // 获取配置的转换工具( mencoder.exe)的存放路径字符串mencoderpath = request.getSession()。getservletContext()。getRealPath(“/tools/tools/tools/mencoder.exe”); avipath = transfmediatool.processavi(mencoderpath,filedirs.getabsolutepath(),codcavipath); fileend = this.getFileext(codcavipath); } if(avipath!= null){// flv if(this.checkmediatype(fileEnd)){try {//设置转换为flvflv格式后文件的保存路径stringcodcfilepath = logorealpathdir + file.separator.separator + separator + newfileName + newfileName +“ .flv”; // 获取配置的转换工具( ffmpeg.exe)的存放路径字符串ffmpegpath = request.getSession()。getServletContext()。getRealPath(“/tools/tools/ffmpeg.exe”); transfmediatool.processflv(ffmpegpath,avipath,codcfilepath); filedir = logopathdir + newfileName +“ .flv”; builder = new StringBuilder(FileDir); FinalFileDir = Builder.Substring(1); } catch(异常E){e.printstacktrace(); }} entity.setsize(size); Entity.SetPath(FinalFileDir); Entity.setTitleorig(name); entity.setTitlealter(newfileName); Timestamp Timestamp = new Timestamp(System.CurrentTimeMillis()); entity.setuploadtime(时间戳);返回实体; } else {return null; }} else {return null; }} / ** * * * * * @param fileName * @return * / private boolean checkfiletype(string fileName){iterator <string> type = arrays = arrays.aslist.aslist(wallefiles).Iterator().iterator(); while(type.hasnext()){字符串ext = type.next(); if(filename.tolowercase()。endswith(ext)){返回true; }}返回false; } / ** *视频类型判断(flv) * * @param fileName * @return * / private boolean checkmediatype(string fileEnd){iterator <string> type = arrays = arrays = arrays.aslist(wellyflv).Iterator(); while(type.hasnext()){字符串ext = type.next(); if(fileend.equals(ext)){返回true; }}返回false; } / ** *视频类型判断(avi) * * @param fileName * @return * / private boolean checkavitype(string fileend){iterator <string> type = type = arrays = arrays.aslist(allyavi).Iterator(); while(type.hasnext()){字符串ext = type.next(); if(fileend.equals(ext)){返回true; }}返回false; } / ** * * * * * @return String * / private String getFileext(string fileName){return fileName.substring(filename.lastindexof(“。”)); } / ** *依据原始文件名生成新文件名 * @return * / private string getName(字符串fileName){iterator <string> type = arrays.aslist.aslist(lassionfiles).iterator(); while(type.hasnext()){字符串ext = type.next(); if(filename.contains(ext)){字符串newfileName = filename.substring(0,filename.lastindexof(ext));返回newfilename; } } 返回 ””; } / ** *文件大小,返回kb.mb * * @return * / private string getsize(file file){字符串size =“”;长fileLength = file.length(); Demalformat df = new demalformat(“#。00”); if(fileLength <1024){size = df.format(((double)fileLength) +“ bt”; } else if(fileLength <1048576){size = df.format(((double)fileLength / 1024) +“ kb”; } else if(fileLength <1073741824){size = df.format(((double)fileLength / 1048576) +“ mb”; } else {size = df.format(((double)fileLength / 1073741824) +“ gb”; }返回大小; }}import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;public class TransfMediaTool { /** * 视频转码flv * * @param ffmpegPath * 转码工具的存放路径 * @param upFilePath * 用于指定要转换格式的文件,要截图的视频源文件 * @param codcFilePath * 格式转换后的的文件保存路径 * @return * @throws Exception */ public void processFLV(String ffmpegPath, String upFilePath, String codcFilePath) { // 创建一个List集合来保存转换视频文件为flv格式的命令 List<String> convert = new ArrayList<String>(); convert.Add(ffmpegpath); //添加转换工具路径convert.add(“ - i”); // 添加参数" -I",该参数指定要转换的文件convert.Add(UpFilePath); //添加要转换格式的视频文件的路径convert.add(“ - ab”); convert.add(“ 56”); convert.add(“ -ar”); convert.Add(“ 22050”); convert.add(“ - q:a”); convert.add(“ 8”); convert.Add(“ - r”); convert.add(“ 15”); convert.add(“ - s”); convert.Add(“ 600*500”); / * * convert.add(“ - qscale”); //指定转换的质量convert.add(“ 6”); * convert.add(“ - ab”); //设置音频码率convert.add(“ 64”); convert.Add(“ - ac”); * //设置声道数convert.add(“ 2”); convert.add(“ -ar”); //设置声音的采样频率 * convert.add(“ 22050”); convert.Add(“ - r”); //设置帧频convert.add(“ 24”); * convert.add(“ - y”); // 添加参数" -y",该参数指定将覆盖已存在的文件 */ convert.Add(codcfilepath);尝试{process videoprocess = new ProcessBuilder(convert).redirectRorstream(true).start(); new printstream(videoprocess.getInputstream())。start(); VideoProcess.Waitfor(); } catch(ioexception e1){e1.printstacktrace(); } catch(InterruptedException e){e.printstacktrace(); }} / ** *对ffmpeg无法解析的文件格式(WMV9,RM,RMVB等),先用Mencoder转换为avi(ffmpeg能解析的)格式 * * @param mencoderpath * @param mencoderpath *转码工具的存放路径mencoderpath,字符串upfilepath,字符串codcavipath){// boolean flag = false;列表<string> cormend = new ArrayList <string>(); cormend.Add(mencoderpath); cormend.add(UpFilePath); cormend.add(“ - oac”); cormend.add(“ mp3lame”); cormend.add(“ - lameopts”); cormend.Add(“ Preset = 64”); cormend.add(“ -lavcopts”); cormend.add(“ acodec = mp3:abitrate = 64”); cormend.add(“ - OVC”); cormend.add(“ xvid”); cormend.add(“ - xvidencopts”); cormend.add(“ bitrate = 600”); cormend.add(“ - of”); cormend.add(“ avi”); commend.add(“ - o”); cormend.add(codcavipath);尝试{//预处理进程processBuilder Builder = new ProcessBuilder(); Builder.Command(称赞); Builder.RedirectRorstream(true); //进程信息输出到控制台过程p = builder.start(); BufferedReader br = new BufferedReader(new InputStreamReader(P.GetInputStream()));字符串线= null; while(((line = br.readline())!= null){system.out.println(line); } p.waitfor(); //直到上面的命令执行完,才向下执行返回codcavipath; } catch(异常E){e.printstacktrace();返回null; }}} class printstream扩展了线程{java.io.inputstream __is = null; public printstream(java.io.inputstream is){__is = is; } public void run(){try {while(this!= null){int _ch = __is.read(); if(_ch!= -1)system.out.print((char)_ch);否则休息; }} catch(异常e){e.printstacktrace(); }}}}实体类
导入java.sql.timestamp; public class fileentity {private string type;私人字符串大小;私人字符串路径;私有字符串标题;私人字符串titlealter;私人时间戳上传时间; public String getType(){返回类型; } public void setType(字符串类型){this.type = type; } public String getsize(){返回大小; } public void setSize(字符串大小){this.size = size; } public String getPath(){返回路径; } public void setPath(string path){this.path =路径; } public String getTitleorig(){return titleorig; } public void settitleorig(string titleorig){this.titleorig = titleorig; } public String getTitlealter(){返回titlealter; } public void settitlealter(string titlealter){this.titlealter = titlealter; } public Timestamp getUploadTime(){返回上传时间; } public void setUploadtime(timestamp uploadtime){this.uploadtime = uploadTime; }}}总结
以上所述是小编给大家介绍的java上传视频实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!