zoukankan      html  css  js  c++  java
  • 文件上传:简单的demo

    /*
    接收前台传来的视频,把视频放到服务器中,并把视频地址保存到数据库
    */
    @RequestMapping(value = "/vms/media_main/saveVideo.do")
    public void saveVideo(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest req, HttpServletResponse res) {

    Videoinfo videoinfo = new Videoinfo();
    String date = DateUtil.dateToString("yyyyMMdd",new Date());
    String path = "/home/nginx/vod/videos/upload/"+date+"/"+username;
    String name = file.getOriginalFilename(); //上传时的文件名
    int k = name.lastIndexOf(".");
    String videoname = name.substring(0,k);
    String format = name.substring(k+1).toUpperCase();//获取文件类型
    Date date1 = new Date();
    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //创建一个图片名字
    String fileName = new Date().getTime()+(int)(Math.random()*100)+name.substring(name.lastIndexOf("."));
    InputStream is;
    OutputStream os;
    String video = "" ;
    try {
    is = file.getInputStream();
    File paths = new File(path);
    if (!paths.exists()) {
    paths.mkdirs();
    }
    os = new FileOutputStream(new File(path,fileName));
    int len = 0;
    byte[] buffer = new byte[400];
    while((len=is.read(buffer))!=-1){
    os.write(buffer,0,len);
    }
    os.close();
    is.close();

    video = "videos/upload/"+date+"/"+username+"/"+fileName;

    videoinfo.setPath(video);
    videoinfo.setFormat(format);
    videoinfo.setVideoname(videoname);
    videoinfo.setCreatetime(format1.format(date1));
    videoinfo.setStatus(1);
    videoService.addSave(videoinfo);

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    <form action="" enctype="multipart/form-data" method="post">
     上传文件:<input type="file" name="file"><br/> 
    <input type="submit" value="提交"> 
    </form>
  • 相关阅读:
    Java Project和Web Project 区别
    ScannerTest-------double string
    ScannerDemo------string int
    clearfix 清除浮动的标签
    bootstrap 的布局
    <span>元素
    反省
    Django中ifequal 和ifnotequal的使用
    IndexError: list index out of range的错误原因
    python2和python3同时存在电脑时,安装包时的的命令行
  • 原文地址:https://www.cnblogs.com/jietz0407-com/p/6494166.html
Copyright © 2011-2022 走看看