页面 :
: ENCTYPE = "Multipart/Form-Data"
<%@ page language = "java"import = "java.util.*"pageencoding = "utf-8"%> <%string path = request.getContextPath (); String BasePath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/";%> <! doctype html public "-// w3c // dtd html 4.01 Transitional // en"> <html> <href = "<%href =" rel = "외부 nofollow"> <title> </title> <meta http-equiv = "pragma"content = "no-cache"> <meta http-equiv = "cache-control"content = "no-cache"> <meta http-equiv = "컨텐츠"0 "> <meta http- equiv ="keywords " content = "keyword1, keyword2, keyword3"> <meta http-equiv = "description"content = "이것은 내 페이지입니다"> </head> <div> <div> <div> <div align = "center"> <h1> </h1> </div> <hr> <form id = "upload" "action ="action = "upload." ENCTYPE = "multipart/form-data"> <div align = "center"> <div> <input type = "file"name "="file "id ="file "> <br> <input type ="value = "value"> </div> </div> </form> </div> </body> </html>
后台 :
제어 장치
import javax.servlet.http.htttp.httpservletrequest; import model.fileentity; import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.sprameframework.web.bind.annotation.requestparam; import org.spramframework.web.bind.annotation.responsebody; import org.springframework.web.multiptile org.springframework.web.servlet.modelandview;@controller@requestmapping ( "/uploadflv") public class uploadcontroller {@requestmapping (value = "/upload", method = {requestmethod.post, requestmethod.get}) @ResponseBody Public ModelView Upload (@RequestAm (value) " multipartfile multipartfile, httpservletrequest 요청, modelmap map) {string message = ""; fileentity entity = new FileEntity (); fileUploadTool fileUploadTool = new FileUploadTool (); {entity = fileUploadTool.createFile (multipartFile, request)을 시도합니다. if (entity! = null) {// service.savefile (엔티티); 메시지 = ";"; map.put ( "엔티티", 엔티티); map.put ( "결과", 메시지); } else {message = "上传失败"; map.put ( "결과", 메시지); }} catch (예외 e) {e.printstacktrace (); } return new ModelAndView ( "result", map); }}工具类
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.htttp.httpservletrequest; import modome.fileentity; import; org.springframework.web.multipart.multipartfile; public class fileUploadtool {transfmediatool transfmediatool = new transfmediatool (); // 500m 개인 정적 정적 긴 UPLOAD_MAXSIZE = 800 * 1024 * 1024; // private static string [] allowfiles = { ".rar", ".rar", ".doc", ".docx", ".zip", ".pdf", ".txt", ".swf", ".xlsx", ".gif", ".jpg", "jbmp", "", "", "", "", "", "", "". " ".flv", ".ppt", ".avi", ".mpg", ".wmv", ".3gp", ".mov", ".asf", ".asx", ".vob", ".wmv9", ".rmvb"}; // 允许转码的视频格式 ay (ffmpeg) private static string [] allowflv = { ".avi", ".mpg", ".wmv", ".3gp", ".mov", ".Asf", ".Asx", ".vob"}; // 允许的视频转码格式 (mencoder) private static string [] allowavi = { ".wmv9", ".rm", ".rmvb"}; public fileentity createFile (MultipartFile MultipartFile, httpservletrequest 요청) {fileentity entity = new FileEntity (); 부울 bflag = 거짓; 문자열 filename = multipartfile.getoriginalfilename (). toString (); // 判断文件不为空 if (multipartFile.getSize ()! = 0 &&! multipartfile.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) {String logopathdir = "/video/"; 문자열 logorealPathDir = requestSession (). getServletContext (). getRealPath (logopathDir); // 上传到本地磁盘 // String logorealPathDir = "e :/upload"; 파일 logosavefile = 새 파일 (logorealPathDir); if (! logosavefile.exists ()) {logosavefile.mkdirs (); } 문자열 이름 = filename.substring (0, filename.lastIndexof ( ".")); System.out.println ( "文件名称 :" + 이름); // 新的文件名 string newFilename = this.getName (filename); // string fileend = this.getFileExt (filename); // 绝对路径 string filenamedirs = logorealPathDir + file.separator + newFilename + 파일 엔드; System.out.println ( "保存的绝对路径 :" + filenamedirs); 파일 filedirs = 새 파일 (filenamedirs); // 转入文件 try {multipartfile.transferto (filedirs); } catch (delegalstateException e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); } // intity.settype (파일 엔드); 문자열 filedir = logopathdir + newfilename + 파일 엔드; StringBuilder Builder = New StringBuilder (Filedir); 문자열 FinalFiledir = builder.substring (1); // size size string size = this.getSize (filedirs); // 源文件保存路径 String avipath = filedirs.getAbsolutepath (); // avi // 부울 플래그 = false; if (this.checkavitype (fileEnd)) {// avi 格式后文件的保存路径 문자열 codcavipath = logorealpathdir + file.separator + newfilename + ".avi"; // 获取配置的转换工具 (mencoder.exe) 的存放路径 string mencoderpath = requestsession (). getServletContext (). getRealPath ( "/툴/mencoder.exe"); avipath = transfmediatool.processavi (mencoderpath, filedirs.getabsolutepath (), codcavipath); fileend = this.getfileext (codcavipath); } if (avipath! = null) {// 转码 flv if (this.checkmediatype (fileend)) {try {// 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 格式后文件的保存路径 设置转换为 设置转换为 设置转换为 设置转换为 设置转换为 worealpilepath = logorealpathdir + file.separator + newfilename + ".flv"; // 获取配置的转换工具 (ffmpeg.exe) 的存放路径 string ffmpegpath = requestsession (). getServletContext (). getRealPath ( "/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 (크기); Entity.setPath (FinalFileDir); Entity.SetTitleOrig (이름); Entity.SetTitLealter (NewFilename); timestamp timestamp = new Timestamp (System.CurrentTimeMillis ()); Entity.setuploadtime (timestamp); 리턴 엔티티; } else {return null; }} else {return null; }} / ** * * * * @param filename * @return * / private boolean checkfileType (String filename) {iterator <string> type = arrays.aslist (allowfiles) .iterator (); while (type.hasnext ()) {String ext = type.next (); if (filename.tolowercase (). endswith (ext)) {return true; }} 거짓을 반환합니다. } / ** * * (flv) * * @param filename * @return * / private boolean CheckMediAtype (문자열 파일 엔드) {iterator <string> type = arrays.aslist (allowflv) .iterator (); while (type.hasnext ()) {String ext = type.next (); if (fileend.equals (ext)) {return true; }} 거짓을 반환합니다. } / ** * * (avi) * * @param filename * @return * / private boolean checkavitype (문자열 파일 엔드) {iterator <string> type = arrays.aslist (allestavi) .iterator (); while (type.hasnext ()) {String ext = type.next (); if (fileend.equals (ext)) {return true; }} 거짓을 반환합니다. } / ** * * * @return String * / private String getFileExt (String filename) {return filename.substring (filename.lastIndexof ( ".")); } / ** * * * @return * / private string getName (String filename) {iterator <string> type = arrays.aslist (allowfiles) .iterator (); while (type.hasnext ()) {String ext = type.next (); if (filename.contains (ext)) {String newfilename = filename.substring (0, filename.lastindexof (ext)); NewFilename을 반환하십시오. } } 반품 ""; } / ** * * *, 返回 kb.mb * * @return * / private String getSize (파일 파일) {String size = ""; long filelength = file.length (); Decimalformat df = 새로운 Decimalformat ( "#. 00"); if (filelength <1024) {size = df.format ((double) filelength) + "bt"; } else if (filelength <1048576) {size = df.format ((더블) filelength / 1024) + "KB"; } else if (filLength <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 (문자열 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"); * //. conver.add ( "2"); convert.add ( "-ar"); // 设置声音的采样频率 * convert.add ( "22050"); convert.add ( "-r"); // 设置帧频 convert.add ( "24"); * convert.add ( "-y"); // 添加参数" -y 添加参数" 添加参数", 该参数指定将覆盖已存在的文件 */ convert.add (codcfilepath); try {process videoprocess = new ProcessBuilder (convert) .redirecterrorstream (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 upfilepath *, 要截图的视频源文件 * @param codcfilepath * @ @retrow expercata (string * / strow processati). MencoderPath, String UpfilePath, String Codcavipath) {// boolean flag = false; list <string> promend = new ArrayList <string> (); mendend.add (mencoderpath); premend.add (upfilepath); mendend.add ( "-OAC"); remend.add ( "mp3Lame"); ormend.add ( "-lamepts"); mendend.add ( "preset = 64"); mendend.add ( "-lavcopts"); ormend.add ( "acodec = mp3 : abitrate = 64"); mendend.add ( "-ovc"); ormend.add ( "xvid"); ormend.add ( "-xvidencopts"); ormend.add ( "bitrate = 600"); ormend.add ( "-of"); mendend.add ( "avi"); mendend.add ( "-o"); mendend.add (codcavipath); try {// processBuilder builder = new ProcessBuilder (); Builder.command (칭찬); builder.redirecterrorstream (true); // process 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 (); 널 리턴; }}} 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 (); }}}实体类
import java.sql.timestamp; public class fileentity {private String 유형; 개인 문자열 크기; 개인 문자열 경로; 개인 문자열 TitleOrig; 개인 문자열 titlealter; 개인 타임 스탬프 업로드 타임; 공개 문자열 gettype () {return type; } public void settype (문자열 유형) {this.type = 유형; } public String getSize () {return size; } public void setsize (문자열 크기) {this.size = size; } public String getPath () {return path; } public void setpath (문자열 경로) {this.path = 경로; } public string getTitleOrig () {return titleorig; } public void settitleorig (String Titleorig) {this.titleorig = TitleOrig; } public String getTitLealter () {return titlealter; } public void settitlealter (String titlealter) {this.titlealter = titlealter; } public timestamp getUploadtime () {return ubloadtime; } public void setuploadtime (timestamp uploadtime) {this.uploadtime = 업로드 타임; }}总结
以上所述是小编给大家介绍的 Java a a, 希望对大家有所帮助, 如果大家有任何疑问请给我留言, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!