国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Java上傳視頻實例代碼

Java上傳視頻實例代碼

2021-03-17 13:59smart_hwt JAVA教程

本文通過實例代碼給大家講解了java上傳視頻功能,代碼分為頁面前臺和后臺,工具類,具體實例代碼大家通過本文學習吧

頁面:

上傳文件時的關鍵詞:enctype="multipart/form-data"

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ 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>
<head>
  <base href="<%=basePath%>" rel="external nofollow" >
  <title>上傳視頻</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
</head>
<body>
  <div class="panel panel-default">
    <div class="panel-body">
      <div class="panel-heading" align="center"><h1 class="sub-header h3">文件上傳</h1></div>
        <hr>
      <form class="form-horizontal" id="upload" method="post" action="uploadflv/upload.do" enctype="multipart/form-data">
        <div class="form-group" align="center">
          <div class="col-md-4 col-sm-4 col-xs-4 col-lg-4">文件上傳
            <input type="file" class="form-control" name="file" id="file"><br>
            <input type="submit" value="上傳">
          </div>
        </div>
      </form>
    </div>
  </div>
</body>
</html>

后臺:

controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import javax.servlet.http.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.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
@RequestMapping("/uploadflv")
public class UploadController {
  @RequestMapping(value = "/upload", method={RequestMethod.POST,RequestMethod.GET})
  @ResponseBody
  public ModelAndView upload(@RequestParam(value = "file", required = false) MultipartFile multipartFile,
      HttpServletRequest request, ModelMap map) {
    String message = "";
    FileEntity entity = new FileEntity();
    FileUploadTool fileUploadTool = new FileUploadTool();
    try {
      entity = fileUploadTool.createFile(multipartFile, request);
      if (entity != null) {
//        service.saveFile(entity);
        message = "上傳成功";
        map.put("entity", entity);
        map.put("result", message);
      } else {
        message = "上傳失敗";
        map.put("result", message);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new ModelAndView("result", map);
  }
}

工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
  private static long upload_maxsize = 800 * 1024 * 1024;
  // 文件允許格式
  private static String[] allowFiles = { ".rar", ".doc", ".docx", ".zip",
      ".pdf", ".txt", ".swf", ".xlsx", ".gif", ".png", ".jpg", ".jpeg",
      ".bmp", ".xls", ".mp4", ".flv", ".ppt", ".avi", ".mpg", ".wmv",
      ".3gp", ".mov", ".asf", ".asx", ".vob", ".wmv9", ".rm", ".rmvb" };
  // 允許轉碼的視頻格式(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 request) {
    FileEntity entity = new FileEntity();
    boolean bflag = false;
    String 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/";
      String logoRealPathDir = request.getSession().getServletContext().getRealPath(logoPathDir);
      // 上傳到本地磁盤
      // String logoRealPathDir = "E:/upload";
      File logoSaveFile = new File(logoRealPathDir);
      if (!logoSaveFile.exists()) {
        logoSaveFile.mkdirs();
      }
      String name = fileName.substring(0, fileName.lastIndexOf("."));
      System.out.println("文件名稱:" + name);
      // 新的文件名
      String newFileName = this.getName(fileName);
      // 文件擴展名
      String fileEnd = this.getFileExt(fileName);
      // 絕對路徑
      String fileNamedirs = logoRealPathDir + File.separator + newFileName + fileEnd;
      System.out.println("保存的絕對路徑:" + fileNamedirs);
      File filedirs = new File(fileNamedirs);
      // 轉入文件
      try {
        multipartFile.transferTo(filedirs);
      } catch (IllegalStateException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      // 相對路徑
      entity.setType(fileEnd);
      String fileDir = logoPathDir + newFileName + fileEnd;
      StringBuilder builder = new StringBuilder(fileDir);
      String finalFileDir = builder.substring(1);
      // size存儲為String
      String size = this.getSize(filedirs);
      // 源文件保存路徑
      String aviPath = filedirs.getAbsolutePath();
      // 轉碼Avi
//      boolean flag = false;
      if (this.checkAVIType(fileEnd)) {
        // 設置轉換為AVI格式后文件的保存路徑
        String codcAviPath = logoRealPathDir + File.separator + newFileName + ".avi";
        // 獲取配置的轉換工具(mencoder.exe)的存放路徑
        String mencoderPath = request.getSession().getServletContext().getRealPath("/tools/mencoder.exe");
        aviPath = transfMediaTool.processAVI(mencoderPath, filedirs.getAbsolutePath(), codcAviPath);
        fileEnd = this.getFileExt(codcAviPath);
      }
      if (aviPath != null) {
        // 轉碼Flv
        if (this.checkMediaType(fileEnd)) {
          try {
            // 設置轉換為flv格式后文件的保存路徑
            String codcFilePath = logoRealPathDir + File.separator + newFileName + ".flv";
            // 獲取配置的轉換工具(ffmpeg.exe)的存放路徑
            String ffmpegPath = request.getSession().getServletContext().getRealPath("/tools/ffmpeg.exe");
            transfMediaTool.processFLV(ffmpegPath, aviPath,  codcFilePath);
            fileDir = logoPathDir + newFileName + ".flv";
            builder = new StringBuilder(fileDir);
            finalFileDir = builder.substring(1);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        entity.setSize(size);
        entity.setPath(finalFileDir);
        entity.setTitleOrig(name);
        entity.setTitleAlter(newFileName);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        entity.setUploadTime(timestamp);
        return entity;
      } 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;
      }
    }
    return false;
  }
  /**
   * 視頻類型判斷(flv)
   *
   * @param fileName
   * @return
   */
  private boolean checkMediaType(String fileEnd) {
    Iterator<String> type = Arrays.asList(allowFLV).iterator();
    while (type.hasNext()) {
      String ext = type.next();
      if (fileEnd.equals(ext)) {
        return true;
      }
    }
    return false;
  }
  /**
   * 視頻類型判斷(AVI)
   *
   * @param fileName
   * @return
   */
  private boolean checkAVIType(String fileEnd) {
    Iterator<String> type = Arrays.asList(allowAVI).iterator();
    while (type.hasNext()) {
      String ext = type.next();
      if (fileEnd.equals(ext)) {
        return true;
      }
    }
    return false;
  }
  /**
   * 獲取文件擴展名
   *
   * @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));
        return newFileName;
      }
    }
    return "";
  }
  /**
   * 文件大小,返回kb.mb
   *
   * @return
   */
  private String getSize(File file) {
    String size = "";
    long fileLength = file.length();
    DecimalFormat df = new DecimalFormat("#.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";
    }
    return size;
  }
}
 
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);
    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
   *      格式轉換后的的文件保存路徑
   * @return
   * @throws Exception
   */
  public String processAVI(String mencoderPath, String upFilePath, String codcAviPath) {
//    boolean flag = false;
    List<String> commend = new ArrayList<String>();
    commend.add(mencoderPath);
    commend.add(upFilePath);
    commend.add("-oac");
    commend.add("mp3lame");
    commend.add("-lameopts");
    commend.add("preset=64");
    commend.add("-lavcopts");
    commend.add("acodec=mp3:abitrate=64");
    commend.add("-ovc");
    commend.add("xvid");
    commend.add("-xvidencopts");
    commend.add("bitrate=600");
    commend.add("-of");
    commend.add("avi");
    commend.add("-o");
    commend.add(codcAviPath);
    try {
      // 預處理進程
      ProcessBuilder builder = new ProcessBuilder();
      builder.command(commend);
      builder.redirectErrorStream(true);
      // 進程信息輸出到控制臺
      Process p = builder.start();
      BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line = null;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
      p.waitFor();// 直到上面的命令執行完,才向下執行
      return codcAviPath;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}
class PrintStream extends Thread {
  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);
        else
          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

實體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.sql.Timestamp;
public class FileEntity {
  private String type;
  private String size;
  private String path;
  private String titleOrig;
  private String titleAlter;
  private Timestamp uploadTime;
  public String getType() {
    return type;
  }
  public void setType(String type) {
    this.type = type;
  }
  public String getSize() {
    return size;
  }
  public void setSize(String size) {
    this.size = size;
  }
  public String getPath() {
    return path;
  }
  public void setPath(String path) {
    this.path = 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 uploadTime;
  }
  public void setUploadTime(Timestamp uploadTime) {
    this.uploadTime = uploadTime;
  }
}

總結

以上所述是小編給大家介紹的Java上傳視頻實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://www.cnblogs.com/smart-hwt/archive/2018/01/10/8256836.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产精品国产三级国产aⅴ原创 | 欧美精品1区2区3区 日本电影中文字幕 | 国产日韩欧美综合 | 亚洲三级黄色 | 很黄很色很爽的视频 | 久久久亚洲国产天美传媒修理工 | 久在线视频 | 日韩午夜在线视频 | 欧美日韩一区在线观看 | 亚洲精品久久久久久动漫 | 国产人体视频 | 日韩av在线不卡 | 亚洲综合一区二区 | 亚洲久草 | 日本久久久久久久久久久久 | 中文字幕亚洲欧美日韩在线不卡 | 青青草在线视频免费观看 | 久久99精品久久久久久琪琪 | 成人深夜免费视频 | 亚洲欧美中文字幕 | 羞羞视频网 | 亚洲高清视频网站 | 久久久久国产精品免费免费搜索 | 欧美色综合天天久久综合精品 | 91网在线观看 | 欧美一区二区公司 | 免费成人在线观看视频 | 在线观看成人 | 国产在线网 | av影音资源 | 精品在线视频一区 | 一区二区三区在线播放 | 欧美一级在线观看 | 一区二区亚洲 | 九九热免费观看 | 亚洲福利网站 | 国产精品福利电影网 | 在线四区 | 国产在线观看一区 | 人人干人人看 | 国产精品久久久久久久一区探花 |